This article brings you a summary of the new digital methods in ES6 (a must-read). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
This article introduces the new numerical methods in ES6 (ECMAScript 6).
This article will introduce you to the new methods and constants of adding Number
data type. Of course, the methods employed here are not entirely new, but they can already be moved directly within and/or (e.g. isNaN()). We will practice this with some examples.
Number.isInteger()
The first method I want to introduce is Number.isInteger()
. It's new to JavaScript, and you may have defined and used this method before. It determines whether the value passed to the function is an integer. If the function value is true, this method returns, if false, it exits. The implementation of this method is very simple and is native JavaScript syntax. One of the ways to rewrite this function is:
Number.isInteger = Number.isInteger || function (number) { return typeof number === 'number' && number % 1 === 0; };
Just for fun, I rewrote this function to use a completely different method:
Number.isInteger = Number.isInteger || function (number) { return typeof number === 'number' && Math.floor(number) === number; };
Although both of the above methods can determine Whether the passed parameters are integers, but they do not comply with the ECMAScript 6 specification. So, if you want to rewrite strictly according to ES6 specifications, please start with the following syntax:
Number.isInteger(number)
The parameter number represents the value to be tested.
An example of using this method is shown below:
// prints 'true' console.log(Number.isInteger(19)); // prints 'false' console.log(Number.isInteger(3.5)); // prints 'false' console.log(Number.isInteger([1, 2, 3]));
This method is supported by Node.js and all modern browsers, except Internet Explorer. If you need to support older browsers, you can use a polyfill, such as the one available on the Mozilla Developer Network for Firefox. Take a look at the code below:
if (!Number.isInteger) { Number.isInteger = function isInteger (nVal) { return typeof nVal === 'number' && isFinite(nVal) && nVal > -9007199254740992 && nVal <h2 id="Number-isNaN">Number.isNaN() </h2><p> If you have written JavaScript code before, this method will not be unfamiliar to you. JavaScript has a method called isNaN() exposed through the window object. This method is used to determine whether the test value is equal to NaN. It returns true, otherwise it returns false. However, there is a problem with calling window.isNaN() directly. When the test value is forced to be converted to a number, this method will return a true value. To give you a concrete idea of the problem, all of the following statements will return: <strong>true</strong></p><pre class="brush:php;toolbar:false"> // prints 'true' console.log(window.isNaN(0/0)); // prints 'true' console.log(window.isNaN('test')); // prints 'true' console.log(window.isNaN(undefined)); // prints 'true' console.log(window.isNaN({prop: 'value'}));
What you probably want is a method that only returns true when the passed value is NaN. This is why ECMAScript 6 introduced Number.isNaN()
. Its syntax is as follows:
Number.isNaN(value) 这value是您要测试的值。此方法的一些示例用法如下所示: // prints 'true' console.log(Number.isNaN(0/0)); // prints 'true' console.log(Number.isNaN(NaN)); // prints 'false' console.log(Number.isNaN(undefined)); // prints 'false' console.log(Number.isNaN({prop: 'value'}));
As you can see, testing the same values we get different results.
This method is supported by Node and all modern browsers, except Internet Explorer. If you want to support other browsers, a very simple polyfill for this method is as follows:
Number.isNaN = Number.isNaN || function (value) { return value !== value; };
NaN is the only non-self value in JavaScript, meaning it is the only value that is not equal to itself.
Number.isFinite()
This method has the same background as the previous method. In JavaScript, there is such a method window.isFinite()
, which is used to test whether the passed value is a finite number. Unfortunately, it also returns a true value that is coerced to a number, an example like this:
// prints 'true' console.log(window.isFinite(10)); // prints 'true' console.log(window.isFinite(Number.MAX_VALUE)); // prints 'true' console.log(window.isFinite(null)); // prints 'true' console.log(window.isFinite([]));
For this reason, in ECMAScript 6 there is a method called isFinite(). The syntax is as follows:
Number.isFinite(value)
value is the value you want to test. If you test the same value from the previous snippet, you can see the results are different:
// prints 'true' console.log(Number.isFinite(10)); // prints 'true' console.log(Number.isFinite(Number.MAX_VALUE)); // prints 'false' console.log(Number.isFinite(null)); // prints 'false' console.log(Number.isFinite([]));
This method is supported by Node and all modern browsers, except Internet Explorer. You can find its polyfill on the methods page on MDN.
Number.isSafeInteger()
Number.isSafeInteger() is a brand new addition to ES6. It tests whether the passed value is a safe integer, in which case it returns true. A safe integer is defined as an integer that satisfies the following two conditions:
The number can be represented exactly as an IEEE-754 double
number The IEEE-754 representation cannot be the result of rounding any other integer to fit the IEEE-754 representation.
According to this definition, a safe integer is from -(2 to the power of 53-1)contained to 2 to the power of 53-1All integers contained.
Number.isSafeInteger(value) 这value是您要测试的值。此方法的一些示例用法如下所示: // prints 'true' console.log(Number.isSafeInteger(5)); // prints 'false' console.log(Number.isSafeInteger('19')); // prints 'false' console.log(Number.isSafeInteger(Math.pow(2, 53))); // prints 'true' console.log(Number.isSafeInteger(Math.pow(2, 53) - 1));
Number.isSafeInteger() is supported in all modern browsers, except Internet Explorer. The polyfill for this method was taken from es6-shim by Paul Miller as:
Number.isSafeInteger = Number.isSafeInteger || function (value) { return Number.isInteger(value) && Math.abs(value) <p> Please note that this polyfill relies on the Number.isInteger() method discussed previously, so you need to do the latter polyfill to use this method. </p><p>ECMAScript 6 also introduces two related constant values: <strong>Number.MAX_SAFE_INTEGER</strong> and <strong>Number.MIN_SAFE_INTEGER</strong>. The former represents the largest safe integer in JavaScript, which is 2 to the power of 53 - 1, while the latter represents the smallest safe integer, which is - (2 to the power of 53 - 1). </p><h2 id="Number-parseInt-and-Number-parseFloat">Number.parseInt() and Number.parseFloat() </h2><p>Number.parseInt() and Number.parseFloat() methods both belong to the same section, because unlike the ones mentioned in this article To other similar methods, they already existed in previous versions of ECMAScript. Therefore, you can use them in the same way as you currently do, and get the same results. The syntax is as follows: </p><pre class="brush:php;toolbar:false"> // Signature of Number.parseInt Number.parseInt(string, radix) // Signature of Number.parseFloat Number.parseFloat(string)
where string represents the value to be parsed and radix is the radix string you want to use for conversion.
The following code snippet shows example usage:
// Prints '-3' console.log(Number.parseInt('-3')); // Prints '4' console.log(Number.parseInt('100', 2)); // Prints 'NaN' console.log(Number.parseInt('test')); // Prints 'NaN' console.log(Number.parseInt({})); // Prints '42.1' console.log(Number.parseFloat('42.1')); // Prints 'NaN' console.log(Number.parseFloat('test')); // Prints 'NaN' console.log(Number.parseFloat({}));
Node和所有现代浏览器都支持这些方法,Internet Explorer除外。如果您想要使用它们,您可以简单地调用它们的全局方法,如下所示:
// Polyfill Number.parseInt Number.parseInt = Number.parseInt || function () { return window.parseInt.apply(window, arguments); }; // Polyfill Number.parseFloat Number.parseFloat = Number.parseFloat || function () { return window.parseFloat.apply(window, arguments); };
相关推荐:
The above is the detailed content of Summary of the new numerical methods in ES6 (must read). For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software