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.
I hope you found it useful. Thanks for reading. 🙏
Let's get connected! You can find me on:
- Medium: https://medium.com/@nhannguyendevjs/
- Dev: https://dev.to/nhannguyendevjs/
- Substack: https://nhannguyendevjs.substack.com/
- Linkedin: https://www.linkedin.com/in/nhannguyendevjs/
- X (formerly Twitter): https://twitter.com/nhannguyendevjs/
Top comments (0)