How to Convert String to Number in Node.js?

30-Sep-2022

.

Admin

How to Convert String to Number in Node.js?

Hello Friends,

This article will provide some of the most important example of how to convert string to number in node.js. This tutorial will give you a simple example of convert string to number in node.js. you will learn to convert string to number in nodejs. we will help you to give an example of node.js convert string to a number. Here, Creating a basic example of nodejs convert a string to a number.

This example is how to convert string to number in nodejs. I will explain simply convert string to number using node js. Node js provides various ways to convert a string value into a number.

You can convert a string to a number in Node.js using any of these two methods: Number(), parseInt().

So, let's start following example with output:

Install Node JS


This step is not required; however, if you have not created the node js app, then you may go ahead and execute the below command:

mkdir my-app

cd my-app

npm init

Example 1:

Use the Number object, in a non-constructor context (without the new keyword):

This takes care of the decimals as well.

const count = Number('1234')

console.log(count);

Output:

1234

Example 2:

This is a separators between digits:

In the case you need to parse a string with decimal separators, use Intl.NumberFormat instead.

const count = Number('10,000')

console.log(count);

Output:

NaN

const count = Number('10.000')

console.log(count);

Output:

10

Example 3:

Use parseInt().

Good solution for integers is to call the parseInt() function:

const count = parseInt('1234', 10)

console.log(count);

Output:

1234

I hope it can help you...

#Node JS