Skip to main content

Command Palette

Search for a command to run...

Simple ways to convert a string into a number

Published
β€’1 min read
Simple ways to convert a string into a number
D

I am 17 years old and a young passionate and self-taught frontend web developer and have an intention to become a successful developer. I usually write about JavaScript and Web Development and share some tips in the articles.

Hello Folks πŸ‘‹

What's up friends, this is SnowBit here. I am a young passionate and self-taught front-end web developer and have an intention to become a successful developer.

Today, I am here with a basic topic for my beginner friends and javascript revisers. Happy Reading!


In this article, I will be discussing a few methods to convert a string into a number.

parseInt()

const x = "5"

parseInt(x) // 5

You can easily convert a string to a number with parseInt(). If you pass any word or a letter in parseInt() then it will return Nan - Not a Number

Number()

In JavaScript, the Number() method converts the value to a number and if the value failed to convert then it will return NaN.

const x = "5"

Number(x)

Plus (+) unary operator

const x = "5"

const num = +x

Using the + operator alone before an element indicates a math operation and tries to convert the element to a number, and if it fails to do - it will return NaN


Thank you for reading, have a nice day! Your appreciation is my motivation 😊

A

Those are really useful ways, thanks for sharing !!!

D

I am glad that my article helped you :)

Happy Coding and Reading!

More from this blog

D

Dhairya Shah

119 posts

Simple ways to convert a string into a number