Join Digital Nomads and Remote Workers to Ask Questions, Share Experiences, Find Remote Jobs and Seek Recommendations.

JavaScript: Shorthand Tricks for Numbers

Integer division by the power of two

// Longhand
Math.floor(x/2);
// Shorthand
x >> 1;

Double Bitwise NOT for Math.floor()

You can use double bitwise NOT operator to replace Math.floor() for positive numbers. While you need to be-careful not to use double bitwise NOT operator for negative number.

Math.floor(2.9) === 2  // Longhand
~~2.9 === 2  // Shorthand
// Don't use "Double Bitwise NOT" for negative number
Math.floor(-3.5)  // -4
~~(-3.5) // -3 (The result difference)

Further Reading: http://rocha.la/JavaScript-bitwise-operators-in-practice

Infinity Shorthand

[Infinity,-Infinity] // Longhand
[1/0,-1/0]           // Shorthand

Decimal Base Exponent

10000000 // Longhand 
1e7     // Shorthand
// All the below will return true
1e0 === 1;
1e1 === 10;
1e2 === 100;
1e3 === 1000;

We Work From Anywhere

Find Remote Jobs, Ask Questions, Connect With Digital Nomads, and Live Your Best Location-Independent Life.