0
Why null==undefined and null == 0 but undefined!= 0
)))
3 ответов
+ 3
JavaScript is weird and funny,lets debug this.
null represents an object/variable with no value so its undefined
0 is null since 0 has no value,anything plus 0 is anything so 0 is null.
Then 0 is not undefined.Its a valueless number,we know what 0 is,we just dont know its value:: valueless!
+ 2
== does type coercion, so the value on the left is coerced into the same type as the value on the right.
undefined is a state that indicates the programmer forgot to set/define a variable (missing or unset variable).
The language has no idea what you mean by whatever you just did. Something fails to exist.
null is a state where the programmer deliberately set a value.
The language knows what you mean (something exists), but its value is absent.
typeof null returning "object" is considered a bug.
You may want to read this article for more information:
http://2ality.com/2011/03/javascript-values-not-everything-is.html
Also in 5. Related content, the link
"null is not an object" - comment on Stack Overflow
0
that is really funny)) only js has null and undefined. in other languages there is only one of them))