Math.floor() Function in Javascript, Uses, Limitations, Type Coercion

Published on | by
Reza Baharvand
Updated on
Image for Math.floor() Function in Javascript, Uses, Limitations, Type Coercion

Introduction

The Math.floor() function Rounds a number down to its closest integer, for example, Math.floor(5.99) returns 5.

What is Math.floor() in Javascript?

Math is a Javascript Built-in object with properties and methods for Mathematical function, constants.

The Math.floor() is one of the Static Methods in Javascript that takes a Number and rounds it down.

console.log(Math.floor(5)); // 5 console.log(Math.floor(7.999)); // 7 console.log(Math.floor(7.009)); // 7 console.log(Math.floor(-3.95)); // -4 console.log(Math.floor(-3.05)); // -4

From the Example, you can see that Math.floor() returns a Number and it is always the largest integer less than or Equal to the provided number.

Math.floor() and Type Coercion

One of the known features of Javascript is the "Type Coercion". Many people might think it is just a bad design, But in my opinion, if you know the type and also what a method is going to return, you should not be worried.

The type coercion happens when one type is converted into another. For example, a Number can turn into String.

console.log(Math.floor(27.99)); // 27 // But Also console.log(Math.floor("27.99")); // 27 // And even console.log(Math.floor("27.99" - 10)); // 17

So the Math.floor() function expects a Number as an argument but if you provide a number as String type, it converts the string to an integer and returns an integer? whaaaa?

Calculating gif

Limitation

One of the caveats of the Math.floor() function is passing null as an argument. You probably expect it to return NaN (not a number), which is something that logically should be true but it returns 0 instead!

console.log(Math.floor(null)); // 0

There are other Static methods for Math that might be useful to you based on your project requirements. I will be writing an article to cover different Methods and create a small ROI calculator shortly, so stay tuned.


Recent Posts

Image for Coin Flip Simulator With Javascript And CSS Animations

Coin Flip Simulator With Javascript And CSS Animations

In this tutorial, you will learn to build a small app to toss a coin using HTML, CSS, and Javascript. The possible outcomes after flipping a coin are either "Head" or "tail". The probability of getting head or tail is 50%.

Image for Use Math.PI To Calculate Area And Circumference Of A Circle

Use Math.PI To Calculate Area And Circumference Of A Circle

The "Math.PI" property returns the PI "π" constant, approximately equal to 3.14159. For example, you can calculate the circumference and surface area of a circle using "Math.PI".

Image for Check If A Function Exists Before Calling With The typeof Operator

Check If A Function Exists Before Calling With The typeof Operator

If you are sharing scripts from different modules or you have a large codebase, you can use the `typeof` operator to check if a function exists.

Image for Create A Toggle Button In Javascript To Manipulate The DOM With The toggle() Method

Create A Toggle Button In Javascript To Manipulate The DOM With The toggle() Method

The "toggle()" is a method of the "DOMTokenList". It removes something like a "class" name from the list, and returns "false". If the "class" name does not exist, it will add it to the list and return "true".