Last Updated: December 26, 2018
·
1.457K
· pedrochaves

Using bitwise-or to convert string to number

You can save a few bytes in your JavaScript code by using the | (bitwise-or) operator instead of parseInt.

var num = "123" | 0; // Returns 123

This code above is equivalent to writing

var num = parseInt("123", 10);

Just be careful about your hexadecimals and octals, this technique does saves some bytes but works kinda like parseInt without the second argument.

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