Differences between undefined, null, and NaN in JS, as well as interview questions about object attribute assignment, undefinednan
(1) In the following three cases, the return type of typeof is undefined.
-- When the variable is not initialized
-- When the variable is undefined
-- When the function does not return a specific value (undefined is returned when the function does not return a value)
(2) Null type
Undefined is derived from null, soUndefined = null
Undefined is a variable declared but not initialized,
Null indicates an object that does not exist.
(3) NaN Value
Is a special value, indicating a non-Number (Not a Number). If the type conversion fails, NaN is returned.
-- NaN is not equal to oneself, that isNaN = NaN is false
-- Judge whether NaN uses isNaN ();
(3) In fact, Null, NaN, and undefined are the default initial values of the variable. Different variable types give different initial values:
-- Int, uint-0
-- Boolean-false
-- Number-NaN
-- String, Array, Object-null
-- The variable type is not specified-undefined
<Script> document. write (typeof (a1); // undefinedvar a2; document. write (typeof (a2); // undefinedvar a3 = []; document. write (typeof (a3); // objectvar a4 ={}; document. write (typeof (a4); // objectvar a5 = null; document. write (typeof (a5); // objectvar a6 = document. getElementById ("this id is not available"); document. write (typeof (a6); // objectvar a7 = new Object; document. write (typeof (a7); // object </script>
(4) An interview question about objects in JS (the core is to pay attention to the object's attribute name, with the JS identifier (a and B) the property names are directly converted to ["object Object"], so they are all the same ):
For detailed analysis, see object creation and attribute access in JS.
<script>var a=new Object;var b=new Object;var c=new Object;c[a]=a;c[b]=b;alert(c[a]==a);//falsealert(c[a]===a);//falsealert(c[a]===b);//truealert(c[b]===b);//true</script>
<script>var a=new Object;var b=new Object;var c=new Object;c["a"]=a;c["b"]=b;alert(c["a"]==a);//truealert(c["a"]===a);//truealert(c["a"]===b);//falsealert(c["b"]===b);//true</script>
References:
Detailed analysis of differences between js and undefined types, undefined and null
Differences between undefined, null, and NaN in js
JavaScript Undefined, difference between the Null type and the NaN Value