JavaScript parseInt() and Number() difference case study

JavaScript parseInt() and Number() difference case study

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(string, radix)
The second parameter indicates the base used. We usually use decimal, but there may also be octal or hexadecimal. In order to avoid parsing errors for strings starting with "0" and "0x", various JavaScript programming specifications require that the value of the second parameter must be clearly given, such as parseInt("123",10).

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:
  • Javascript Basic Tutorial: Data Types (Number)
  • JavaScript Data Structure Number
  • Introduction to JavaScript Number and Math Objects
  • javascript converts other types to Number type
  • Detailed explanation of the 19-digit Number type precision loss problem in JS
  • Detailed explanation of JS numerical Number type
  • Number data type in JavaScript

<<:  Mysql query database capacity method steps

>>:  Win10 configuration tomcat environment variables tutorial diagram

Recommend

Two ways to open and close the mysql service

Method 1: Use cmd command First, open our DOS win...

In-depth discussion on auto-increment primary keys in MySQL

Table of contents Features Preservation strategy ...

Analysis of Alibaba Cloud CentOS7 server nginx configuration and FAQs

Preface: This article refers to jackyzm's blo...

Vue implements accordion effect

This article example shares the specific code of ...

Introduction to document.activeELement focus element in JavaScript

Table of contents 1. The default focus is on the ...

Vue implements three-dimensional column chart based on echarts

The three-dimensional column chart consists of th...

Use of Linux telnet command

1. Introduction The telnet command is used to log...

Implementation steps for building a local web server on Centos8

1 Overview System centos8, use httpd to build a l...

How to use selenium+testng to realize web automation in docker

Preface After a long time of reading various mate...

Comparing Document Locations

<br />A great blog post by PPK two years ago...

A little-known JS problem: [] == ![] is true, but {} == !{} is false

console.log( [] == ![] ) // true console.log( {} ...

Introduction to installing and configuring JDK under CentOS system

Table of contents Preface Check and uninstall Ope...