Hi Friends 👋,
Welcome To Infinitbility! ❤️
To convert a string to a number in node js, you can use the +
plus operator it’s the fastest way to convert a string to a number.
In the following example, we will take the sample string variable, and convert it into an integer using the plus operator.
1let text = "24325";23// using plus operator4let num = +text;5console.log(num) // 24325
Today, I’m going to show you How do i convert string to number in node js, as above mentioned here, I’m going to use the plus operator and string interpolation.
Let’s start today’s tutorial how do you convert string to number in node js?
In this example, we will do
- take an example of a string variable
- use the plus operator
- print the converted number on the page screen
1var http = require("http");23//create a server object:4http5 .createServer(function (req, res) {6 let text = "24325";7 let num = +text;8 res.write(`${num} is ${typeof num}`); //write a response to the client9 res.end(); //end the response10 })11 .listen(8080); //the server object listens on port 8080
In the above node js example, we have taken the example of converting a string to a number and return the convert number and their datatype in the response.
I hope it helps you, All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.