Detailed explanation of value types and reference types in JavaScript

Source: Internet
Author: User
Tags chrome developer

JavaScript variable type

Value types: String, numeric, Boolean, Null, Undefined

Reference types: Arrays, objects, functions

Comparison of value type and reference type

Compare these two types through two instances (Debug environment: Chrome Developer Tool Console)

Value type instance

var a = ' Hello ';
var B = A;
b /* Hello * *
A = ' world ';
b /* Hello * *

The variable assignment procedure for a value type actually allocates a new memory space with the same value but the two do not interfere with each other

Reference type instance

/* Object or array * *
var a = {"X": 0};
var B = A;
b.x; /* 0 * *
a.x = 1;
b.x; /* 1 * *
A = {"X": 2}; /* At this point, a points to another memory space, B's value will not be disturbed.
b.x; /* 1 * *
a.x = 3;
b.x; /* 1 * *
/* Function * *
var a = new Function ("Alert (' Test ')");
var B = A;
b (); /* Execute alert (' Test ') * *

The variable assignment procedure for a reference type is to point two variables to the same memory space, where a change in the value of one variable can cause another variable value to change

Related Article

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.