type

Every value in the JavaScript language belongs to a certain data type. The JavaScript language specifies seven language types. Language types are widely used for variables, function parameters, expressions, function return values, and so on. According to the latest language standards, the seven language types are:

  1. Undefined;
  2. Null;
  3. Boolean;
  4. The String;
  5. Number;
  6. Symbol;
  7. The Object.

Undefined, Null,

Our first question, why do some programming specifications require void 0 instead of undefined? Now let’s look at them separately.

Undefined means Undefined, and its type has only one value, Undefined. Any variable is Undefined and its value is Undefined before assignment. In general, we can use the global variable Undefined (the same variable named Undefined) to express this value, or void to change any expression to Undefined.

However, since undefined is a variable and not a keyword in JavaScript code, which is one of the most recognized design mistakes of the JavaScript language, to avoid inadvertent tampering, I recommend using void 0 to get undefined.

Undefined is the same as Null, which means “defined but Null”. Therefore, in actual programming, we usually do not assign the value of undefined to a variable, so that all variables with the value of undefined are the natural state that has never been assigned.

The Null type also has only one value, Null, which means Null. Unlike undefined, Null is a JavaScript keyword, so you can safely use the Null keyword to retrieve Null values in any code.

Boolean

The Boolean type has two values, true and false, which are used to represent logical truth and false, as well as the keywords true and false to represent both values. This is a very simple type, and I won’t go into it too much.

String

Strings in JavaScript can never be changed. Once a string is constructed, you can’t change the contents of the string in any way, so strings have the characteristics of value types

Number

By definition, non-integer Number types cannot be compared with == (nor with ===). In JavaScript, 0.1+0.2 cannot =0.3:

Console. log(0.1 + 0.2 == 0.3);

The output result here is false, indicating that the two sides are not equal, which is a feature of floating point operation and also a source of confusion for many students. The accuracy of floating point operation leads to the results of the left and right sides of the equation are not exactly equal, but slightly different.

So in fact, what is wrong here is not the conclusion, but the method of comparison, and the correct method of comparison is to use the minimum precision provided by JavaScript:

Console. log(math.abs (0.1 + 0.2-0.3) <= number.epsilon);

The correct way to compare floating point numbers is to check that the absolute value of the difference between the left and right sides of the equation is less than the minimum precision. The result of this code is true.

Symbol

Symbol is a new type introduced in ES6. It is a collection of all non-string object keys. In the ES6 specification, the entire object system is reshaped with Symbol.

We create symbols by using the global Symbol function. For example: var mySymbol = Symbol(“my Symbol “);

Object

Object is the most complex type in JavaScript and one of the core mechanics of JavaScript. Object is an Object. It is a general name for all tangible and intangible objects.

In JavaScript, an object is defined as a “collection of attributes.” Attributes are divided into data attributes and accessor attributes, both of which are key-value structures. Keys can be strings or symbols.

Type conversion

Other operations, such as addition, subtraction, multiplication, and division greater than or less, also involve type conversions. Fortunately, most of the conversion rules are actually quite simple, as shown in the following table: