Why null==undefined and null == 0 but undefined!= 0 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why null==undefined and null == 0 but undefined!= 0

)))

13th May 2018, 6:59 PM
nareko
nareko - avatar
3 Answers
+ 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!
13th May 2018, 7:05 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 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
13th May 2018, 7:44 PM
Kirk Schafer
Kirk Schafer - avatar
0
that is really funny)) only js has null and undefined. in other languages there is only one of them))
13th May 2018, 7:34 PM
nareko
nareko - avatar