JavaScript basic data type and value type reference type description

Source: Internet
Author: User
Tags button type

Lin Bingwen Evankaka original works. Reprint please specify the source Http://blog.csdn.net/evankaka

Summary: This article mainly discusses the basic data types in JavaScript, as well as the difference and use of value types and reference types

I. Basic data Types

The keyword used in JavaScript to declare variables is VAR, which is different from other programming languages, but JavaScript also contains five basic data types (which can be said to be simple data types), namely: Undefined,null,boolean, Number and string. Also contains a complex data type-object.

(1), "undefined"--not stated, or the value of the variable is undefined or uninitialized;

(2), "Boolean"--if the value of this variable is a Boolean type;
(3), "string"-the value is a string type;
(4), "number"-the value is a numeric type;
(5), "Object"--the object or value is null;
typeof This keyword is a must say, because JavaScript is loosely typed, when the variable declaration does not use the corresponding type of keyword, if you want to know the amount of basic data of a variable in the code, you can use TypeOf. It is important to note that TypeOf returns a string type.

(5), "function"--functions.

Example Validation:

1, Undefined
The undefined type has only one value, which is undefined. When the declared variable has not been initialized, the default value for the variable is undefined

function test1 () {var testmessage;alert (typeof testMessage);}

2, Null

The null type also has only one value, which is null. Null is used to denote an object that does not exist and is commonly used to indicate that a function is attempting to return an object that does not exist

function Test2 () {var testMessage = Null;alert (typeof testMessage);}


3, String

String, which can be any text in quotation marks. You can use single or double quotation marks:

function Test3 () {var testMessage = "Hello"; alert (typeof TestMessage)}
4. number

Can be a floating-point number, integer

function test4 () {var testMessage = 12;alert (typeof testMessage)}


5, Boolean

Boolean that has two values of true or false.

function Test5 () {var testMessage = True;alert (typeof testMessage)}
6, Obeject:

objects and arrays, and null as well. objects and arrays can contain different types, including objects and arrays.

function Test6 () {var testMessage = [];alert (typeof TestMessage)}function test7 () {var testMessage = [];alert (typeof TestMessage)}function Test8 () {var testMessage = new Object (); alert (typeof TestMessage)}


7. function

function type

function Test9 () {alert (typeof Test8)}

second, value types and reference types

(1) value type: Numeric, Boolean, null, undefined

value type refers to the simple data segment stored in the stack memory, access by value, operation is the value they actually saved;

(2) Reference type: object, array, function

reference types refer to objects that are stored in the heap memory, meaning that the variables are actually saved in a pointer, which executes another location in memory, where the object is saved, the reference access, and when queried, we need to read the memory address from the stack first. And then find the value stored in the heap memory;

For example, the following are reference types

var cars=   New Array;var person= new Object;

1. Value type instance:



2. Reference type instance




Attention:
Undefined,null, empty string, 0 is equal to false, all can pass! To take the reverse.

Copyright NOTICE: This article for Bo Master Lin Bingwen Evankaka original article, reproduced please indicate the source Http://blog.csdn.net/evankaka

JavaScript basic data type and value type reference type description

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.