Basic types and reference types in JavaScript

Source: Internet
Author: User

I. Overview of basic types and reference types

Values for data types in JS include: base type values and reference type values

Base data type: undefined;null;boolean;number;string

Reference type value: In memory, JS does not allow direct access to memory locations, so when manipulating references instead of actual objects


Second, how to detect the data type

1. Detection of basic data types: Using typeof

var s = "AAA"; alert (typeof s);    

' 2. Reference type ( object type) detection: Using instanceof

Alert (person instanceof Object), alert (person instanceof Array), alert (person instanceof Regexp);

3. Special case: Instanceof detects that object always returns True, and always returns false when detecting a base type (because the base type is not an object)

typeof return function when detecting functions, returning an object when detecting regular expressions


Three, the difference between the basic type and the reference type

1. Reference types can add attributes, and basic types cannot be

2. When copying, the basic type is directly copied a new variable, the new and old two variables are not related;

The reference type also copies the new variable, but the variable is a pointer to the new and old two pointers to the same object

3. Parameter passing of the function: all parameter passing principle is the argument that the external variable is passed to the function by means of copying. Therefore, the operation of parameters inside the function has no effect on the external original variable

The following are examples of parameters as basic types and reference types, respectively:

function Addten (num) {    num + = ten;    return num;} var count = 20;var result = Addten (count);//The internal operation of NUM does not affect the value of external count function SetName (obj) {    obj.name = "Nicholas"; C5/>obj = new Object ();    Obj.name = "Greg";} var person = new Object (), setName (person), alert (person.name)    ; Returns "Nicholas", stating that the name of the person object that still does not affect the external

Basic types and reference types in JavaScript

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.