# Math.floor()

Hi everyone, and welcome to another exciting adventure in Boring JavaScript! Today, we take a look at the Math.floor() method.

Don’t like to read? Then view our video.

## Get Off That Floor

The Math.floor() method returns the largest integer less than or equal to a number. For positive numbers, this will be the same as stripping out the decimal portion and returning just the integer part.

```const myCatWeight = 13.2;
const apxCatWeight = Math.floor(myCatWeight);

const myDogWeight = 22.7;
const apxDogWeight = Math.floor(myDogWeight);

console.log(`My cat weighs about \${apxCatWeight} kilograms.`);
console.log(`My dog weighs about \${apxDogWeight} kilograms.`);

/* output
My cat weighs about 13 kilograms.
My dog weighs about 22 kilograms.
*/
```

For negative numbers, the decimal portion is stripped and 1 is subtracted (because that new number is less than the original number. For example:

```const day1Temp = -1.6;
const apxDay1Temp = Math.floor(day1Temp);

const day2Temp = -5.1;
const apxDay2Temp = Math.floor(day2Temp);

console.log(`The temperature for Day 1 was about \${apxDay1Temp}C.`);
console.log(`The temperature for Day 2 was about \${apxDay2Temp}C.`);

/* output
The temperature for Day 1 was about -2C.
The temperature for Day 2 was about -6C.
*/
```

Notice that in both instances that the number wasn’t ’rounded’. Math.floor() will not round the number for you – it merely strips the decimal portion.

## Oddities and Peculiarities

What about numbers that are not ‘numbers’? How will Math.floor() deal with them? Let’s find out!

### Strings

If the string can be coerced into a number, then Math.floor() will work. Otherwise, it will return a NaN (with one exception – keep reading).

```console.log(Math.floor("1.2"));
console.log(Math.floor("TheVirtuoid"));

/* output
1
NaN
*/
```

### Objects, undefined, NaN

Each of these will return NaN.

```console.log(Math.floor({ myCat: 3 }));
console.log(Math.floor(undefined));
console.log(Math.floor(NaN));

/* output
NaN
NaN
NaN
*/
```

### Booleans, Null, Blank String

It’s not what you think! Like a string, Math.floor() will attempt to coerce the value into a number. Boolean True and False are coerced into 1 and 0, respectively. Null and a Blank String? Those are ‘falsey’ values, and therefore are interpreted as Boolean False – which get coerced into 0.

```console.log(Math.floor(false));
console.log(Math.floor(true));
console.log(Math.floor(null));
console.log(Math.floor(""));

/* output
0
1
0
0
*/
```

While Math.floor() will do exactly what you expect it to do, be careful to make sure you only pass numbers as the argument. Otherwise, you may not get what you expect.

## The Video

Here is a video we’ve made on the subject.

## Shameless Plug

You can find us everywhere!

Check out all our videos at: https://www.boringjavascript.com

Check out everything at: https://www.thevirtuoid.com

Categories: Boring JavaScript Javascript

Tagged as: ### thevirtuoid

Web Tinkerer. No, not like Tinkerbell.

Creator of the game Virtuoid. Boring JavaScript. Visit us at thevirtuoid.com

This site uses Akismet to reduce spam. Learn how your comment data is processed.