Integer Division in JavaScript

Last Updated On Saturday 9th Apr 2022

JavaScript Integer Division

What is integer division?

What integer division is and how it works. In integer division, the remainder is discarded.

For example,

  • Division of 1 / 2 results in 0.5, integer division of the same expression results in .
  • Likewise, 36 / 5 will result in 7 in integer division, but 7.2 in the floating-point division.

Math.floor() function returns the largest integer less than or equal to a given number.

	console.log(Math.floor(7.2));
// output: 7

console.log(Math.floor(1.5));
// output: 1
	

Getting Division Answer in Int

  • Sometimes that value could be in JSON as well, means from a JSON data file.
  • You need to deal with it as if it were a number. But the value that you’re getting is a String
  • Need to convert it from the string into a number
  • Global Methods parseInt & parseFloat
	let stringInt = "44";

console.log(parseInt(stringInt));
// output: 44

console.log(parseInt(25));
// output: 25

console.log(parseInt(99.44));
// output: 99

console.log(parseInt("52 balls in the tray"));
// output: 52