Recently in the front of re-learning, I was afraid to forget or not really understand the course, so HERE is a summary ~(the above picture is the structure diagram of JS summarized by the teacher for your reference).

The types mentioned here are all run-time types. Here’s a quote from the teacher about run-time types

Runtime types are the types we use during the actual execution of our code. All type data belongs to one of the seven types. Any data generated during the execution of JavaScript code, from variables, parameters, return values, to intermediate expression results, has a runtime type.

A couple of questions

Why do some programming specifications require void 0 instead of undefined? Does a string have a maximum length? Isn't 0.1 + 0.2 equal to 0.3? Why isn't that the case in JavaScript? What is the new Symbol in ES6? Why do methods added to objects work on primitive types?Copy the code

Seven language types

  1. Undefined
  2. Null
  3. Boolean
  4. String
  5. Number
  6. Symbol(ES6)
  7. Object

Undefined and Null

Undefined means Undefined and has only one value Undefined. Any variable that is declared has only one type Undefined and one value Undefined before it is assigned

So why use void 0 instead of undefined? As undefined is not a reserved word in JS, it can be used as a variable name, so undefined can be changed, and void operator will result in undefined, so we can use void 0 instead of undefined to prevent modification. Void 0 is 3 bytes less than undefined, which can be verified by printing the respective length.

In ES5, however, undefined is only a read-only property of a global object, so it will not be overridden in older browsers, but replacing undefined with void 0 is not completely unnecessary. Many JS compression tools use void 0 instead of undefined.

The Null type also has a value Null, which means “defined but Null”. Null is the keyword in JS.

Boolean

There are only two values, true and false, which are logically true or false.

String

A String has a maximum length of 2^ 53-1. This maximum length is not the length of a character, because String does not mean “String”, but the UTF16 encoding of the String. Our string operations charAt, charCodeAt, length and other methods are for UTF16 encoding. So, the maximum length of a string is actually influenced by the encoding length of the string.

Number

The Number type in javaScript has 18437736874454810627(that is, 2^64-2^53+3) values

The Number type in javascript basically conforms to the rules of IEEE 754-2008 for double precision floating-point numbers (according to the definition of double precision floating-point numbers, the valid range of integers in Number type is 0x1FFFFFFFFFFFFF to 0x1FFffffFFF, So Number is not an exact representation of integers outside this range), but JavaScript specifies several miles to express a few additional scenarios (such as introducing infinity in order not to divide by zero) :

  • NaN: occupied 9007199254740990, which was originally a number in accordance with IEEE rules;
  • Infinity: Infinity;
  • -infinity: negative Infinity

JS has +0 and -0 points, in the division operation need to pay attention to distinguish, see is positive infinity or negative infinity;

By floating-point definition, non-integer Number types cannot be compared with == (===), as in

console.log(0.1+0.2= =0.3)//false
Copy the code

This is the precision of the floating-point arithmetic problem, need to first converted to binary again in counting, due to some in the converted to binary floating-point Numbers is infinite, but can only support of 64 double-precision floating-point number 53 binary, so the truncated then converted to a decimal will there is a certain error, but this is not can not compare, There is a minimum precision value in “Number”. In other words, if the error is less than the minimum precision value, the error does not exist.

console.log(Math.abs(0.1+0.2-0.3) < =Number.EPSILON)//true,
Copy the code

Symbol

Create a symbol object

var obj=Symbol("a");
Copy the code

Object

In the definition of JS, an object is a “collection of attributes”, which can be divided into data attributes and accessor attributes, both of which are deconstructed by key-value. The key can be a string or Symbol type

Several basic types in JS have a corresponding object type

  • Number
  • Boolean
  • String
  • Symbol

So we need to recognize that 3 and new Number(3) are completely different values, one of Number type and one of object type

console.log(typeof 3)//number
console.log(typeof (new Number(3)))//object
Copy the code

The Number,Boolean, and String constructors are dual-use. When used with new, they create an object. When called directly, they cast

The Symbol function is special and cannot be called with new, but it is still the constructor of the Symbol object

Js language is designed to blur the relationship between basic types and objects, and object methods can be used on basic types, such as


    console.log("abc".charAt(0)); //a
Copy the code

That’s not possible in the Java language, and that’s because. Provides a boxing operation to construct a temporary object based on a primitive type.

The last

Answer the first few questions

Why do some programming specifications require void 0 instead of undefined? Void 0 is 3 bytes less than undefined; 2. Is there a maximum length of a string for which undefined can be modified as a variable in older browsers? Yes, 2^53-1 isn't 0.1 + 0.2 equal to 0.3? Why isn't that the case in JavaScript? As a result of the floating-point arithmetic rules, we can determine what Symbol is added to ES6 by the minimum precision difference of Number. Symbol can generate a unique value, avoiding key duplication in key-value structures. Why can the method of adding objects be used on primitive types? Js can do automatic boxing, building a temporary object based on the primitive type to call object methods.Copy the code

If any error, welcome to leave a message ~