Differences between basic types and reference types in JavaScript

Source: Internet
Author: User

Code Listing 1:
var a=3.14;
var b=a;
a=4;
alert (b);

Code 2
var a=[1,2,3];
var b=a;
a[0]=99;
alert (b);

Why does code 1 show 3.14, why does the value of a not change? Obviously, after executing the second line of code, we re-assigned a value. Is it because the value of the first row given to a is an underlying type that cannot be changed?
That's why code 2 shows the value is 99,2,3, why the value of a is changed, because the array is a reference type, the reference type does not have a fixed size, it does not really assign to whom, but just assigns a value to B a reference, tell the variable b where to find it, is that it? Can you change the value of a reference type to a primitive type?
Who can give me a plain explanation! Thank you.

Differences between basic types and reference types in JavaScript

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.