[Javascript] Correctly Type-Checking Numbers

There are two ways to correctly type checks number:

console.log(typeof 99.66); // number
console.log(Object.prototype.toString.call(99) === '[object Number]'); // true

 

When using Number.prototype methods, some of them return String as result:

console.log((99.12345678).toPrecision(5)); // 99.123

We can see this string:

console.log(typeof (99.12345678).toPrecision(5)); // string

 

If we want to conver this string type of number:

console.log(parseFloat((99.12345678).toPrecision(5))); // 99.123

 

Last in Chrome dev tool, you can see that, Number is shown in Blue color, and string is shown in Black color:

console.log(parseFloat((99.12345678).toPrecision(5)));
console.log((99.12345678).toPrecision(5));

posted @ 2019-06-18 21:19  Zhentiw  阅读(167)  评论(0编辑  收藏  举报