The original type of JavaScript

Source: Internet
Author: User

original value and reference value


In ECMAScript, a variable can hold two types of values, that is, the original value and the reference value.

    • The original value (primitive value) is a simple data field stored in the stack (stack), that is, their values are stored directly in the location of the variable access.
    • The reference value (reference value) is the object stored in the heap (heap), that is, the value stored in the variable is a pointer (point) to the memory of the storage object.


Original Type


There are 5 primitive types in ECMAScript, namely Undefined,Null,Boolean, number, and String. ECMAScript defines the term type as a collection, and each primitive type defines the range of values 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, which is the variable or value to check . For example:

var sName = "Zhangsan"; alert (typeof SName); Output Stringalert (typeof 88); Output number

Calling the TypeOf operator on a variable or value returns one of the following values:

    • "Undefined" if the variable is of type undefined
    • "Boolean" If the variable is of type Boolean
    • "Number" If the variable is of type number.
    • "String" If the variable is of type string.
    • "Object" If the variable is a reference type or a null type.


Undefined type


The Undefined type has only one value, which is Undefined.

Undefined values generally have three cases:

    • Declared variable uninitialized value
    • Non-declared variable
    • When a function has no explicit return value


declared variable not initialized


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

var Sname;alert (typeof SName);  Alert (SName = = undefined);

The previously declared sname, there is no initial value. The variable will be given undefined, which is the literal of the undefined type.


non-declared variable


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

Undeclared variable, the default value is given to undefined


when a function has no explicit return value


Undefined returned when the function has no explicit return value or no return value

function TestFunction () {//No Return}

For example

function TestFunction () {return;} Alert (testfunction = = undefined); Output "true"

Null Type


The value of a null type has only one null, which is its literal. The value undefined is actually derived from null, so ECMAScript defines them as equal.

Alert (Null = = undefined); Output true

Although the two values are equal, they have different meanings. A undefined is a value that is assigned to a variable when it is declared but not initialized, and null is used to represent an object that does not already exist. If the function or method is to return an object, the returned object is usually null when it is not found.


Boolean type


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


Number type


Number type numbers can represent either 32-bit integers or 64-bit floating-point digits. Any number that is entered directly is considered to be the literal of the numeric type.

var iNum = 123;

Integers can be expressed as octal or hexadecimal literals.

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

var iNum = 070;  070 is equals, in decimal

To create a hexadecimal literal, the first digit must be 0, followed by the letter x and then any hexadecimal digit (0-9a-f). The letters can be in uppercase or lowercase.

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

Although all integers can be represented as octal or hexadecimal literals, all mathematical operations return a decimal result .


String Type


What is unique about the String type is that it is the only original type that has no fixed size. You can store 0 or more Unicode characters in a string, represented by a 16-bit integer (Unicode is an international character set).


Each character in the string has a specific position, starting at position 0, the second character 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 a No. 0 character, E is the 1th character, and the last character O is 4th (that is, the string length 5 minus 1) characters

String literals can be declared by double quotation marks (") or single quotation marks (').

For example, the following two lines are valid declarations:

var sColor1 = "Blue"; var SColor2 = ' Blue ';



Attach all the codes

<script>//typeof var sName = "Zhangsan"; alert (typeof SName); Output Stringalert (typeof 88); Output number//Undefinedvar Sname;alert (typeof sName);   Output undefined    alert (sName = = "undefined");  Output Truealert (typeof SAge); Output Undefinedfunction TestFunction () {return;} Alert (testfunction = = undefined); Output true//Nullalert (null = = undefined); Output true//Numbervar iNum = 070;  070 is equals Decimalalert (INum), var iNum1 = 0x1f;alert (INUM1), var iNum2 = 0x2b;alert (iNum2);//Stringvar SColor1 = "Blue"; alert (SColor1); var sColor2 = ' Blue '; alert (SCOLOR2);</script>


The original type of JavaScript

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.