“This is the 15th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
JS advanced series of articles
- Pure functions of functional programming
- Curritization of functional programming
- Combinatorial functions of functional programming
- Explain the classes in ES6
- Knowing these things about ES6, daily development is better
- Data structures added in ES6
ES7 grammar
ES7 is also called ES2016. The syntax after ES6 is called ES6+. Let’s take a look at the new syntax in ES7. These are the newly added includes method and the exponentiation operator **.
array includes
The includes method is used to determine whether an element exists in an array
Previously, array.indexof was used to determine whether a value was in an Array. After ES7, array.includes was used to determine whether a value was in an Array.
ES5 uses the array.indexof method:
const names = ["abc"."cba"."nba"."mba".NaN];
if (names.indexOf("cba")! = = -1) {
console.log("Contains ABC elements");
}
Copy the code
The array. includes method is used in ES7:
const names = ["abc"."cba"."nba"."mba".NaN];
if (names.includes("cba".2)) {
console.log("Contains ABC elements");
}
Copy the code
Judgments about NaN
In the includes method, the presence of NaN values can be determined in the array, while indexOf cannot be determined correctly.
const names = ["abc"."cba"."nba"."mba".NaN];
if (names.indexOf(NaN)! = = -1) {
console.log("Contains NaN");
}
if (names.includes(NaN)) {
console.log("Contains NaN");
}
Copy the code
Exponentiation operator
The exponentiation operator (**) returns the result of adding the first operand to the power of the second. It is equivalent to math.pow, which you can understand as the + and – operators.
In ES5, we used the math.pow method to power a number.
const result = Math.pow(3.3);
Copy the code
Now, we can exponentiate directly using the ** operator. And it’s a lot simpler.
const result2 = 3 ** 3;
Copy the code
You can also use BigInts as the operand.
What is BigInt (ES11)
Is a new basic data type in ES11 that can be an arbitrarily large integer. It addresses the limitation of the Number type.
When a Number is greater than the largest integer that the Number type can represent, the Number is rounded. That would compromise the reliability and security of the program.
9007199254740992 = = = 9007199254740993; // trueCopy the code
To solve this limitation, you can use the BigInt type to solve the problem.
BigInt("9007199254740998"); // 9007199254740998n
Copy the code
Instead of using the BigInt constructor to create a value of type BigInt, you can also add n after the number.
console.log(typeof 9007199254740998n) // bigint
Copy the code
The difference between Number and Number
BigInt
You can’t useMath
Methods in objects- Can’t and
Nunber
The computations BigInt
turnNumber
Type may lose precision