Original JavaScript type

Source: Internet
Author: User

Original JavaScript type
Original Value and reference value

 

 

In ECMAScript, variables can be stored in two types of values: Original Value and reference value.

The original value is a simple data field stored in the stack, that is, their values are directly stored in the access location of the variable. The reference value is an object stored in heap. That is, the value stored in the variable is a pointer to the memory of the stored object.

 

Original Type

 

 

ECMAScript has five primitive types:Undefined,Null,Boolean,NumberAndString. ECMAScript defines the term type as a set, and each primitive type defines the range of the value it contains and its literal representation. ECMAScript provides the typeof operator to determine whether a value is within a certain range.

 

Typeof Operator

 

The typeof operator has a parameter, that is, the variable or value to be checked.. For example:

var sName = “zhangsan;alert(typeof sName); //output stringalert(typeof 88); //output number

 

When the typeof operator is called for a variable or value, one of the following values is returned:

Undefined: If the variable is an Undefined boolean, if the variable is a Boolean number, if the variable is a Number. String, if the variable is String type. Object. If the variable is of the reference type or Null type.

 

 

Undefined type

 

 

The Undefined type has only one value, that is, undefined.

The undefined value generally has three conditions:

When no explicit return value is returned for declared variable uninitialized value undeclared variable functions

 

 

The declared variable is not initialized.

 

 

When the declared variable is not initialized, the default value of this variable is undefined.

var sName;alert(typeof sName);  alert(sName == undefined);

The previously declared sName has no initial value. This variable is assigned undefined, that is, the literal volume of the Undefined type.

 

Undeclared Variables

 

When an undeclared variable is used, the default value of the variable is undefined.

var sName;alert(typeof sName); // output undefinedalert(typeof sAge); // output undefined

Undefined variable. The default value is undefined.

 

When the function does not explicitly return values

 

When the function does not return a specific value or does not return a value, the returned value is undefined.

function testFunction() {// no return}

For example

 

function testFunction() {return;}alert(testFunction == undefined); // output “true”

 

Null type

 

 

A Null value has only one null value, that is, its literal volume. The undefined value is actually derived from null, so ECMAScript defines them as equal.

alert(null == undefined); // output true

Although these two values are equal, they have different meanings. Undefined indicates the value of a variable that is declared but not granted to it during initialization. null indicates an object that does not exist. If a function or method returns an object, null is usually returned if the object cannot be found.

 

Boolean Type

 

The Boolean type is one of the most common types in ECMAScript. It has two values: true and false. In some cases, 0 can also be converted to false.

 

Number Type

 

A Number of 'number' can represent both a 32-bit integer and a 64-bit floating point Number. Any Number directly entered is treated as a Number literal.

 

var iNum = 123;

 

An integer can be expressed as an octal or hexadecimal literal.

The first digit of the octal literal must be 0, and the subsequent digit can be any octal digit (0 to 7 ).

 

var iNum = 070;  // 070 is equals 56 in decimal

 

To create a hexadecimal literal, the first digit must be 0, followed by the letter x, followed by any hexadecimal number (0-9A-F ). These letters can be in upper or lower case.

 

var iNum1 = 0x1f; // 31var iNum2 = 0x2B; // 43

 

 

Although all integers can be expressed as octal or hexadecimal literal values, all mathematical operations return decimal results.

 

String type

 

The unique feature of the String type is that it is the only primitive type with no fixed size. You can use strings to store 0 or more Unicode characters, expressed by 16-bit integers (Unicode is an international character set ).

 

Each character in a string has a specific position. The first character starts from 0, the second character is at position 1, and so on. The position of the last position of the string is the length of the string minus one.

 

Hello

The length of this string is 5.

H is 0th characters, e is 1st characters, and the last character o is 4th (that is, the string length is 5 minus 1) characters

 

The string literal can be declared by double quotation marks (") or single quotation marks.

For example, the following two rows are valid declarations:

 

var sColor1 = blue;var sColor2 = 'blue';


 

 


 

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.