DEV Community

Discussion on: Vanilla JavaScript String to Number

Collapse
 
bhagatparwinder profile image
Parwinder 👨🏻‍💻

Keep in mind that this also works for hexadecimal.

console.log(+"0xF"); // 15
console.log(+"321"); // 321

And so does ~~

console.log(~~"0xF"); // 15
console.log(~~"321"); // 321
Collapse
 
dailydevtips1 profile image
Chris Bongers

I Actually didn't know about the ~~ What is it called?

Thread Thread
 
rrdlpl profile image
R • Edited

it's the NOT bitwise operator. It changes the 1 by 0 and 0 by 1. In that example, he is doing it twice.

~ 5 => ~0101 => 1010
~~5 => ~(~0101) => ~(1010) => 0101 => 5

w3schools.com/js/js_operators.asp