Description of reference types of basic javascript data types and value types

Source: Internet
Author: User

Description of reference types of basic javascript data types and value types

I. Basic Data Types

In javascript, the keywords used to declare variables are var. This is different from other programming languages, but javascript also contains five basic data types (simple data types ), they are: Undefined, Null, Boolean, Number, and String. It also contains a complex data type-Object.

(1) "undefined" -- undefined, or the variable value is undefined or not initialized;

(2) "boolean" -- if the value of this variable is of the boolean type;
(3), "string" -- the value is of the string type;
(4), "number" -- the value is a number;
(5), "object" -- the object or value is null;
The keyword typeof must be mentioned, because javascript is loose and does not use the keyword corresponding to the type During Variable declaration, if you want to know the basic data size of a variable in the code, you can use typeof. Note that typeof returns the string type.

(5), "function" -- function.

Instance verification:

 

<Html> 

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

 

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

2. Null

The Null type also has only one value, that is, null. Null indicates an existing object. It is often used to indicate that a function attempts to return a non-existing object.

 

 

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

 

It can be a floating point or integer.

 

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

 

5. boolean

Boolean, with two values true or false.

function test5(){var testMessage = true;alert(typeof testMessage)}
6. obeject:

Objects and arrays, and null. Objects and groups can all 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)}

 

Ii. Value Type and reference type

(1) Value Type: numerical value, Boolean value, null, undefined

The value type refers to the simple data segment stored in the stack memory, accessed by value, and operated on the actually saved values;

 

(2) reference type: object, array, Function

 

The reference type refers to the objects stored in the heap memory. It means that the objects stored in the variables are actually just a pointer. the pointer executes another location in the memory and stores the objects at this location; reference access. When querying, we need to first read the memory address from the stack, and then find the value stored in the heap memory;

For example, the following are all reference types:

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

 

1. Value Type instance:

 

<Html> 

 

2. Reference Type instances

 

<Html> 

 

Note:
Undefined, null, empty string, 0 is equal to false, all can pass! .


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.