Learning objectives:The two functions parseInt() and Number() are most often used to convert a string into a data type, so what are the differences between them? You will learn: The parseInt() function parses the given string into an integer in the specified radix. parseInt('16', 8) = 14 parseInt('10', 8) = 8 parseInt('16', 10) = 16 parseInt('10', 10) = 10 parseInt('16', 16) = 22 parseInt('10', 16) = 16 parseInt parses the string into an integer from the beginning. When it encounters a character that cannot be parsed, it returns the parsed integer part. If the first character cannot be parsed, it returns NaN directly. Number() can be used to perform type conversions when the new operator is not used. If it cannot be converted to a number, NaN is returned. For example, for "123a", parseInt() returns 123, while Number() returns NaN. Different types of strings are converted differently using these two functions: // When the string is composed of numbers, they convert to the same number without any difference let numStr = '123' console.log(parseInt(numStr)) //123 console.log(Number(numStr)) //123 // When the string is composed of letters let numStr = 'abc' console.log(parseInt(numStr)) //NaN console.log(Number(numStr)) //NaN // When the string is composed of numbers and letters let numStr = '123a' console.log(parseInt(numStr)) //123 console.log(Number(numStr)) //NaN // When the string is composed of 0 and digits let numStr = '0123' console.log(parseInt(numStr)) //123 console.log(Number(numStr)) //123 // **When the string contains a decimal point** let numStr = '123.456' console.log(parseInt(numStr)) //123 console.log(Number(numStr)) //123.456 // **When the string is null** let numStr = null console.log(parseInt(numStr)) //NaN console.log(Number(numStr)) //0 // **When the string is '' (empty)** let numStr = '' console.log(parseInt(numStr)) //NaN console.log(Number(numStr)) //0 Learning summary: 1. When the string is composed of numbers, the numbers they convert are the same without any difference; if the string does not contain numbers but only letters, both methods will just return NaN results; when the string is composed of 0 and numbers, all numbers except 0 will be parsed; 2 When the string is composed of numbers and letters ① The letter is at the beginning, and both methods return NaN results ② The letter is not at the beginning. The Number method returns NaN, and the pareseInt method returns the data before the letter 3 parseInt converts non-String values into String type before operation 4 For the rest of the details, refer to the above case This is the end of this article about the detailed case analysis of the difference between JavaScript parseInt() and Number(). For more related content about the difference between js parseInt() and Number(), please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Mysql query database capacity method steps
>>: Win10 configuration tomcat environment variables tutorial diagram
Method 1: Use cmd command First, open our DOS win...
Table of contents Features Preservation strategy ...
Preface: This article refers to jackyzm's blo...
This article example shares the specific code of ...
Download tutorial of mysql-connector-java.jar pac...
Generally speaking, the background color of a web ...
The following is some basic sql knowledge I have ...
Table of contents 1. The default focus is on the ...
The three-dimensional column chart consists of th...
1. Introduction The telnet command is used to log...
1 Overview System centos8, use httpd to build a l...
Preface After a long time of reading various mate...
<br />A great blog post by PPK two years ago...
console.log( [] == ![] ) // true console.log( {} ...
Table of contents Preface Check and uninstall Ope...