Value types in javascript detail _javascript tips

Source: Internet
Author: User

The essence of computer program is to a large extent the machine to the various information (value) operation and reading and writing. In JavaScript, there are several types of values that fall into two broad categories: primitive (base type) and object.

Primitive

There are 5 types of primitive in javascript:

1.Number. All numbers, whether integers or decimals, are of type number.
2.String. The string type.
3.Boolean. Boolean type, True or false.
4.null. This type has only a null value.
5.undefined. This type has only one value undefined.

Object

Any other value in addition to Primitive,javascript is object. Object has the following types:

1.JSON key value pair object. such as {"Name": "Bob", "Age": 42}.
2. Arrays (array). such as [1,4,5,7,9].
3. Functions (function). such as function () {return true;}. There are two forms of function in JavaScript: 1. Executable block of code, 2. The constructor (constructor) of the class. Functions (function) are always objects, regardless of the form in which they exist.

JS with Global object

To facilitate program writing, JavaScript takes a global object that has the following 7 member variables of type object:

1.Math. You can accomplish a series of complex mathematical operations by calling the method of the Math object.
2.Number. You can get some special values by accessing the member variables of the number object.
3.Array. The constructor of the array object.
4.Function. The constructor of the function object.
5.Date. The constructor of the date object.
6.RegExp. The constructor of the regular expression object.
7.Error. The constructor of the error object.

When you write a program, you can use it as a global object because you can access the 7 variables directly.

Immutable and mutable

Primitive and object have a distinct feature: All primitive are immutable, and all of the object is mutable. With the string type as an example, when you edit a string by calling it, JavaScript saves the edited result in a new string object, and the original string object does not change:

Copy Code code as follows:

var s = "Test";
S.touppercase ();//return a new String object "TEST"
Console.log (s);//"Test"--original String s does not

Experiment

In JavaScript, you can get the type of a value by using the TypeOf keyword.

Get the type of number:

Copy Code code as follows:

var n = 42;
Console.log (typeof N);

The program outputs the result as number.

Gets the type of the string:

Copy Code code as follows:

var s = "Test";
Console.log (typeof s);

The program output is string.

Gets the type of the Boolean value:

Copy Code code as follows:

var B = true;
Console.log (typeof B);

The program output is Boolean.

Gets the type of NULL:

Copy Code code as follows:

var x = null;
Console.log (typeof x);

The program should have output null, but actually output object. The reason is that when you use the TypeOf action on a null value, the program returns object: This is a bug that has existed since the first version of JavaScript. There have been some interesting arguments about fixing the bug in the ECMAScript standard-setting process: Http://wiki.ecmascript.org/doku.php?id=harmony:typeof_null The final conclusion: fixing the bug will cause problems for too many sites, so it's not fixed.

Get the type of undefined:

Copy Code code as follows:

var y = undefined;
Console.log (typeof y);

The result of the program output is undefined.

Get the type of the JSON object:

Copy Code code as follows:

var j = {"name": "Bob", "Age": 42};
Console.log (typeof J);

The program outputs the result to object.

Gets the type of the array object:

Copy Code code as follows:

var a = [2,3,5,7,11];
Console.log (typeof a);

The program outputs the result to object.

Gets the type of the function object:

Copy Code code as follows:

var f = function () {return true;};
Console.log (typeof F);

The function object is special, and the typeof operator returns the result as a function.

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.