The JavaScript variable

Source: Internet
Author: User

1base Type value

Description: Simple data segment:undefined,null,number,string, boolean.

2Reference type value

Description: An in-memory object exists.

3Copy variable values

If you copy the value of the base type from one variable to another, a new value is created on the variable object and the value is copied to the location where the new variable is assigned.

Example:

var num1=1;var num2=num1;

Both the NUM1 and num2 values are 1andare independent, changing the num1 will not change num2 the value.

Note: These two variables can participate in any operation without affecting each other.

When a variable copies the value of a reference type to another variable, it also copies the value stored in the variable object into the space allocated for the new variable. The difference is that a copy of this value is actually a pointer to an object stored in the heap. When the copy operation is finished, two variables will actually refer to the same object.

Example:

var obj1={    name:' 123 '}; var obj2=obj1;obj1.name= ' 345 '; obj2.age=3; Console.log (obj2.name); Console.log ( Obj1.age); /*
345 3
*/

Note: These two variables, changing one of the variables, will affect the other variable.

4Detection Type

Typeof:typeof is a powerful helper for detecting basic data types:

Example:

var a=3; Console.log (typeof a); //  Number

However,Typeod is not very useful when detecting objects of reference types, because we do not usually want to know that a value is an object, but rather to know what type of object it is, this time instanceof It's very useful.

Example:

var a=[1,2instanceof Array); // true

The JavaScript variable

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.