Home »
JavaScript
Math.floor() Method with Example in JavaScript
JavaScript | Math.floor() Method: Here, we are going to learn about the floor() method of Math class in JavaScript with Examples.
Submitted by Shivang Yadav, on December 23, 2019
JavaScript | Math.floor() Method
Math.floor() is a function in math library of JavaScript that is used to round down the number passed to the function. The method will return the nearest integer value indeed smaller one than the passed number.
Syntax
Math.floor(val);
Parameters
- val – It represents the value to be rounded down.
Return Value
The return type of this method is number, it returns the a number which is rounded down value of the number passed to the method.
Technical Insights
- JavaScript version: ECMAScript 1
- Browser support: Chrome, Internet Explorer, Mozilla, Safari, Opera mini
Values accepted
Integer, floating point, single value array, numeric string.
Invalid values
Non-numeric string, multi value array, empty variable, empty array all will return NaN (not a number).
Example 1: Valid values for the method
console.log(Math.floor(5.21));
console.log(Math.floor(10.98));
console.log(Math.floor(-3.32));
console.log(Math.floor(0));
console.log(Math.floor("7.18"));
Output
5
10
-4
0
7
Example 2: Invalid values for the method
console.log(Math.floor("IncludeHelp"));
// Output: NaN
console.log(Math.floor(1 + 5i));
// Output: Uncaught SyntaxError: Invalid or unexpected token
JavaScript Math Object Methods »