DEV Community

Nhan Nguyen
Nhan Nguyen

Posted on

Different Ways To Convert String Into Number Using Pure JavaScript

1. parseInt(str): 
Parses a string and returns an integer. It stops parsing when it encounters a non-numeric character.

2. parseFloat(str):
Parses a string and returns a floating-point number. It parses until it encounters an invalid character.

3. Number(str): 
Converts a string to a number. It handles both integers and floating-point numbers.

4. +str: 
Unary plus is a quick way to convert a string to a number. It works similarly to Number(str).

5. str | 0: 
A bitwise OR operation with zero effectively truncates the decimal part, converting a string to an integer.

6. str * 1: 
Multiplying a string by 1 is another way to convert it to a number. It works for both integers and floating-point numbers.

Image description


I hope you found it useful. Thanks for reading. 🙏
Let's get connected! You can find me on:

Top comments (0)