简体   繁体   中英

typeof(1) prints “Number” to the console, but when i ask if typeof(1) == Number, it prints out false, why?

typeof(1) prints "Number" to the console, but when i ask if typeof(1) == Number, it prints out false, why?

console.log(typeof(1));
//Number

console.log(typeof(1) == Number);
//false

typeof returns a string, so check against one

typeof(1) == 'number' // true

// or better without ( ), since you don't need them here
typeof 1 == 'number' // true

and it should be fine. More background info on typeof : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM