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);

Parameter(s):

  • 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 »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.