Explanation of javascript expression evaluation

来源:互联网 发布:帝国cms视频 模版 编辑:程序博客网 时间:2024/06/05 14:11

!! typeof “123” === “string”

What value will the above expression be? If you type it on bowser console, you will get a “false”. The question is, why. To figure it out, you must be clear of the operation precedence in javascript. Refer to MDN, we could see the operation precedence list, from highest to lowest.

Operator type Individual operators member . [] call create instance () new negation/increment ! ~ - + ++ – typeof void delete multiply/divide * / % addition/subtraction + - bitwise shift << >> >>> relational < <= > >= in instanceof equality == != === !== bitwise-and & bitwise-xor ^ bitwise-or | logical-and && logical-or || conditional ?: assignment = += -= *= /= %= <<= >>= >>>= &= ^= |= comma ,

Therefore, for ‘!! typeof “123” === “string”’, typeof operator get executed first, which return “string”, and not(!) followed, returned true because of double not(!!). Finally, === operator has the lowest precedence in the expression, so it’s executed at last. Obviously true === “string” return false. This is how false come out.