Difference Between Number() and parseInt() in Converting Strings?

Number(): Searches the entire string for numbers. If it finds anything else, it will return NaN (short for Not a Number).

parseInt() / parseFloat(): Returns the first number in the string, ignoring the rest. If there are no numbers at the beginning of…


This content originally appeared on DEV Community and was authored by yns

Number(): Searches the entire string for numbers. If it finds anything else, it will return NaN (short for Not a Number).

parseInt() / parseFloat(): Returns the first number in the string, ignoring the rest. If there are no numbers at the beginning of the string, it will return NaN.

let's take an example:

Example 1:
let a = "1hola1";
console.log(Number(a));
console.log(parseInt(a));
output->
NaN
1

Example 2:
let a = "1";
console.log(Number(a));
console.log(parseInt(a));
output->
1
1

In conclusion , We use Number() for the Strings that only contains numbers,the parseInt() does the opposite.


This content originally appeared on DEV Community and was authored by yns


Print Share Comment Cite Upload Translate Updates
APA

yns | Sciencx (2024-06-23T09:10:17+00:00) Difference Between Number() and parseInt() in Converting Strings?. Retrieved from https://www.scien.cx/2024/06/23/difference-between-number-and-parseint-in-converting-strings/

MLA
" » Difference Between Number() and parseInt() in Converting Strings?." yns | Sciencx - Sunday June 23, 2024, https://www.scien.cx/2024/06/23/difference-between-number-and-parseint-in-converting-strings/
HARVARD
yns | Sciencx Sunday June 23, 2024 » Difference Between Number() and parseInt() in Converting Strings?., viewed ,<https://www.scien.cx/2024/06/23/difference-between-number-and-parseint-in-converting-strings/>
VANCOUVER
yns | Sciencx - » Difference Between Number() and parseInt() in Converting Strings?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/23/difference-between-number-and-parseint-in-converting-strings/
CHICAGO
" » Difference Between Number() and parseInt() in Converting Strings?." yns | Sciencx - Accessed . https://www.scien.cx/2024/06/23/difference-between-number-and-parseint-in-converting-strings/
IEEE
" » Difference Between Number() and parseInt() in Converting Strings?." yns | Sciencx [Online]. Available: https://www.scien.cx/2024/06/23/difference-between-number-and-parseint-in-converting-strings/. [Accessed: ]
rf:citation
» Difference Between Number() and parseInt() in Converting Strings? | yns | Sciencx | https://www.scien.cx/2024/06/23/difference-between-number-and-parseint-in-converting-strings/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.