The essence of a computer program can be said to be largely the operation and reading and writing of various information (values) by the machine. In JavaScript, there are multiple types of values, which are divided into two categories: Primitive (primitive type) and Object (object).
Primitive
There are 5 types of Primitive in JavaScript:
1.Number. All numbers, whether integers or decimals, are of type Number.
2.String. String type.
3.Boolean. Boolean type, true or false.
4.null. This type has only one value of null.
5.undefined. This type has only one value undefined.
Object
Except Primitive, any other value in JavaScript is an Object(object). There are several types of Objects:
1.JSON key-value pair object. Such as {"name":"Bob", "age":42}.
2. Array. Such as [1,4,5,7,9].
3. Function. Such as function(){return true;}. There are two forms of functions in JavaScript: 1. Executable code block; 2. Class constructor. No matter what form it exists, a function is always an object.
JS comes with global objects
To facilitate programming, JavaScript comes with a global object, which has the following 7 member variables, all of which are Object:
1.Math. A series of complex mathematical operations can be completed by calling the method of the Math object.
2.Number. Some special values can be obtained by accessing member variables of Number objects.
3.Array. The constructor of an 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 writing programs, since you can directly access the above 7 variables, you can also use them as global objects.
Immutable vs Mutable
Primitive and Object have a clear distinction: all Primitives are Immutable, and all Objects are Mutable. Taking the String type as an example, after calling the String method to edit it, JavaScript will save the edited result in a new String object, and the original String object will not change anything:
The code copy is as follows:
var s = "test";
s.toUpperCase();//return a new String object "TEST"
console.log(s);//"test" -- original String s does not change
experiment
In JavaScript, you can get the type of a certain value by using the typeof keyword.
Get the type of number:
The code copy is as follows:
var n = 42;
console.log(typeof n);
The output result of the program is number.
Get the type of string:
The code copy is as follows:
var s = "test";
console.log(typeof s);
The output result of the program is string.
Get the type of boolean value:
The code copy is as follows:
var b = true;
console.log(typeof b);
The output result of the program is boolean.
Get the type of null:
The code copy is as follows:
var x = null;
console.log(typeof x);
The program should output null, but it actually outputs object. The reason is that when using typeof operation on null values, the program will return object: this is a bug that has existed since the first version of JavaScript. During the formulation of the ECMAScript standard, there were some interesting debates on whether to fix this bug: http://wiki.ecmascript.org/doku.php?id=harmony:typeof_null; the final conclusion is: fixing this bug will cause problems to too many websites, so it will not be fixed for the time being.
Get the undefined type:
The code copy is as follows:
var y = undefined;
console.log(typeof y);
The output result of the program is undefined.
Get the type of JSON object:
The code copy is as follows:
var j = {"name":"Bob", "age":42};
console.log(typeof j);
The output result of the program is object.
Get the type of array object:
The code copy is as follows:
var a = [2,3,5,7,11];
console.log(typeof a);
The output result of the program is object.
Get the type of function object:
The code copy is as follows:
var f = function(){return true;};
console.log(typeof f);
The function object is quite special, and the typeof operator returns the result as function.