A second talk about basic types and reference types in JavaScript (recommended) _javascript tips

Source: Internet
Author: User

Overview of basic types and reference types

The values of data types in JS include: base type values and reference type values

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

Reference type value: In memory, JS does not allow direct access to memory location, so action references rather than actual objects

Second, how to detect data types

1. Detection of basic data types: Using typeof

var s = "AAA"; 
Alert (typeof s); return string

2. Reference type (object type) Detection: Using instanceof

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

3. Special case: instanceof Detection object always returns TRUE, and always returns false when detecting the base type (because the base type is not an object)

Returns a function when the typeof is detected, and returns object when detecting the regular expression

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

1. Reference types can add properties, the base type cannot be

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

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

3. Parameter Pass of function: all the parameter transfer principle is to pass the external variable to the function's parameter by the way of copying. Therefore, the operation of the parameter inside the function has no effect on the external original variable

The following are examples of the basic types and reference types that are validated as arguments:

function Addten (num) { 
num = ten; 
return num; 
} 
var count =; 
var result = Addten (count); 
This internal operation on NUM does not affect the value of external count 
function SetName (obj) { 
obj.name = "Nicholas"; 
obj = new Object (); 
Obj.name = "Greg"; 
} 
var person = new Object (); 
SetName (person); 
alert (person.name); Returns "Nicholas" that describes the name of the person object that is not affected externally

The above is a small series to introduce the basic types of JavaScript and reference types (recommended), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.