Last Updated: February 25, 2016
·
2.139K
· Osvaldo Zonetti

JavaScript: Floor a number like a boss

It's not semantic, but it's interesting.

var test = 2.6;

console.log(~~test); // 2
console.log(test | 0); // 2

OBS: It's not safe either. (Thanks @mlb)

~~9999999999.3; // 1410065407

5 Responses
Add your response

this awesome. must be a bug!?! :-)

over 1 year ago ·

Be careful with this, thought :

~~9999999999.3
// 1410065407
over 1 year ago ·

@mlb Well observed.

Thanks for the warning.

over 1 year ago ·

Completely pointless but awesome.

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside