Some insights about the data types of JS

Source: Internet
Author: User

About the data types in JS this piece, the little brother said some personal views on it, if there is a mistake hope everyone together to help me correct

The data types in JS can be categorized into two categories,

Simple data type: string,number,boolean,null,undefined

Complex data type: Object

In fact, strictly speaking: null is the object type

         alert (typeof (10)); // 10:number        typeof ("abc")); // abc:string        typeof (true)); // True:boolean        typeof (null)); // Null:object        typeof (undefined)); // undefined:undefined

1 default null values for each data type:







var null; // the default empty object of String var obj_num=nan; // the default empty object for number var false; // default Null object for Boolean

But we are generally defined directly:

var obj_str, Obj_num, Obj_bool;

We have this statement, although understand that they want to give them what type, but embarrassed JS, JS can not identify what type of data variables, so JS simply throw a undefined default value to them.

So, for undefined we can be understood to be: no type, no value, white a little bit is in memory do not assign it to the location at all,

Undefined can also be understood as a generic term for all unknown, non-existent types and values of objects.

The difference between 2.undefined and null:

Let's look at a piece of code first:

  null ? Alert (True): Alert (false);     // output: True  null ? Alert (True): Alert (false);     // output: false    

The two of them are different in themselves, and Null is equivalent to an empty box, and undefined is the equivalent of nothing.

If you convert to the same type (which I prefer to understand as ignore types) to compare values, the values of undefined and null are equal,

Undefined is an empty object and NULL is a null value.

It is equivalent to removing the type now, which is equivalent to removing the box, and then they both have nothing,

Let's look at another piece of code:

undefined = 1? Alert (True): Alert (false

What happens when you run this line of code?

Output Result: True

If you're feeling a little surprised, you have to watch it carefully,

Before we say why the output is true, let's look at a piece of code:

if (undefined = 1) {            alert (true);        }         Else {            alert (false);        }

Let everybody down, I just changed a writing, so can see more obvious:

Output True because 1 is true when converted to Boolean,

But, what I want to say is: Why Undefined=1 not error?

The reason is: undefined although and null they are classified as a class, but undefined can be assigned value,

Here I join in some of my personal views:

Undefined is a null object built into JS, so that it takes for granted to assign a value to it,

In this case, NULL itself is a null value, occupy a value in memory, then if we give null assignment, will not error? I guess it's an error.

With this conjecture, I ran the following code:

   null = 1? Alert (True): Alert (false//


This is proving our conjecture, too.

These are some of my simple knowledge of JS data types, and I hope to advise

Some insights about the data types of JS

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: 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.