A detailed introduction to the value types in JavaScript and a detailed introduction to javascript
The essence of a computer program is, to a large extent, the operation and read/write of various information (values) by the machine. There are multiple types of values in JavaScript, which are divided into two categories: Primitive (basic type) and Object (Object ).
Primitive
There are 5 types of Primitive in JavaScript:
1. Number. All numbers, whether integer or decimal, are of the Number type.
2. String. String type.
3. Boolean. Boolean, true, or false.
4. null. This type has only one null value.
5. undefined. This type has only one undefined value.
Object
All values except Primitive in JavaScript are objects ). Objects include the following types:
1. JSON key-Value Pair object. For example, {"name": "Bob", "age": 42 }.
2. array ). For example, [1, 4, 5, 7, 9].
3. functions ). For example, function () {return true ;}. In JavaScript, functions have two forms: 1. executable code blocks; 2. constructor ). No matter which form the function exists, the function is always an object.
JS built-in Global Object
To facilitate programming, JavaScript comes with a global Object. The Global Object has the following seven member variables, all of which are of the Object type:
1. Math. You can call the Math object method to complete a series of complex mathematical operations.
2. Number. You can access the member variables of the Number object to obtain some special values.
3. Array. Constructor of the array object.
4. Function. The constructor of the function object.
5. Date. The constructor of the date object.
6. RegExp. Constructor of the regular expression object.
7. Error. The constructor of the error object.
During programming, these seven variables can be directly accessed and used as global objects.
Immutable and Mutable
Primitive and Object have a distinct feature: ALL Primitive are Immutable, and all objects are Mutable. Taking the String type as an example, after the String method is called to edit it, JavaScript will save the edited result in a New String object, and the original String object will not be changed:
Copy codeThe Code is as follows:
Var s = "test ";
S. toUpperCase (); // return a new String object "TEST"
Console. log (s); // "test" -- original String s does not change
Lab
In JavaScript, you can use the typeof keyword to obtain the type of a value.
Type of the obtained Number:
Copy codeThe Code is as follows:
Var n = 42;
Console. log (typeof n );
The program output result is number.
Obtain the string type:
Copy codeThe Code is as follows:
Var s = "test ";
Console. log (typeof s );
The program output is string.
Type of the Boolean value:
Copy codeThe Code is as follows:
Var B = true;
Console. log (typeof B );
The program output is boolean.
Obtain the null type:
Copy codeThe Code is as follows:
Var x = null;
Console. log (typeof x );
The program should have output null, but actually output the object. The reason is that when typeof is used for the null value, the program will return the object: This is a bug that exists since the first JavaScript version. In the process of developing ECMAScript standards, is there any interesting debate about fixing this bug: http://wiki.ecmascript.org/doku.php? Id = harmony: typeof_null; the final conclusion is: fixing this bug will cause problems for too many websites, so it is not fixed for the moment.
Obtain the undefined type:
Copy codeThe Code is as follows:
Var y = undefined;
Console. log (typeof y );
The program output is undefined.
Obtain the JSON object type:
Copy codeThe Code is as follows:
Var j = {"name": "Bob", "age": 42 };
Console. log (typeof j );
The program outputs an object.
Obtain the type of the array object:
Copy codeThe Code is as follows:
Var a = [2, 3, 5, 7, 11];
Console. log (typeof );
The program outputs an object.
Obtain the function object type:
Copy codeThe Code is as follows:
Var f = function () {return true ;};
Console. log (typeof f );
Function objects are special. The typeof operator returns a function.