The difference between undefined, null, and Nan in JS, as well as the face question of assigning values to object properties

Source: Internet
Author: User

(1) The following three cases typeof return type is undefined

--When the variable is not initialized

--When a variable is not defined

--When the function has no explicit return value (undefined is returned when the function has no return value)


(2) Null type

Undefined is handled by a null derivation, so undefined = = NULL

Undefined is the variable declared but not initialized,

Null indicates an object that does not already exist.


(3) NaN value

is a special value that represents a non-number (not a digit), and the type conversion fails to return Nan

--nan is not equal to itself, that is nan = = Nan is False

--Judge Nan using IsNaN ();


(3) In fact, NULL, Nan, and undefined are the default initial values for variables. Different types of variables, the system gives the initial value is different:

--int,uint-0

--boolean-false

--number-nan

--string,array,object-null

--Unspecified variable type-undefined


<script>document.write (typeof (A1));//undefinedvar A2;document.write (typeof (A2));//undefinedvar a3=[]; document.write (typeof (A3));//objectvar a4={};d ocument.write (typeof (A4));//objectvar A5=null;document.write ( typeof (A5))//objectvar A6=document.getelementbyid ("Without this ID");d ocument.write (typeof (A6));//objectvar a7=new Object ;d Ocument.write (typeof (A7));//object</script>

(4) An interview topic about the object in JS (the core is to pay attention to the object's attribute name, and use the JS identifier (A and B) to make the property name directly converted to ["Object Object"], so all are the same):

For detailed analysis, see: Object creation, 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>


Resources:

JS judge undefined type, undefined,null, the difference between the detailed analysis

The difference between Undefined,null,nan in JS

JavaScript undefined,null type and nan value differences


The difference between undefined, null, and Nan in JS, and the face question of assigning values to an object's properties

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.