Type of boolean expression with comparison operator

if (typeof foo !== 'undefined') {
    // Now we know that foo is defined, we are good to go.
}

typeofevaluated as trueor falsedepending on whether the variable is defined foo.

But, say, if it foo !== 'undefined'is evaluated as true, then typeofof trueshould be evaluated to 'boolean'. Why does he value trueor false?

+4
source share
1 answer

Because the precedence rules for operators typeofand inquality determine that this expression is parsed as

(typeof foo) !== 'undefined'

. MDN . typeof - 16; Inquality 10. typeof , " " .

, undefined ?
+6

Source: https://habr.com/ru/post/1666635/


All Articles