Differences between undefined, null, and NaN in JS, as well as interview questions about object attribute assignment, undefinednan

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.