简体   繁体   中英

What's the difference between “typeof str” and “typeof(str)” in JavaScript?

What is the difference between these two statements?

if (typeof errorMessage !== undefined)
{}

and

if (typeof (errorMessage) !== undefined)
{}

One of them has a pair of entirely superfluous parentheses.

The difference between typeof foo and typeof (foo) is the same as the difference between 1 + 1 and (1) + (1) .


An an aside, the typeof operator will give you a string, so you should be comparing to "undefined" not undefined .

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