Poppy Cooke

Poppy Cooke

1651657095

11 Ways to Convert a String to Number in JavaScript

How to Convert a String to a Number in JavaScript

There are many ways to convert a string into a number using JavaScript. But what does that look like in code?

In this article, I will show you 11 ways to convert a string into a number.

  1. using the Number() function
  2. using the parseInt() function
  3. using the parseFloat() function
  4. using the unary plus operator (+)
  5. multiplying the string by the number 1
  6. dividing the string by the number 1
  7. subtracting the number 0 from the string
  8. using the bitwise NOT operator (~)
  9. using the Math.floor() function
  10. using the Math.ceil() function
  11. using the Math.round() function

How to convert a string to a number in JavaScript using the Number() function

One way to convert a string into a number would be to use the Number() function.

In this example, we have a string called quantity with the value of "12".

const quantity = "12";

If we used the typeof operator on quantity, then it will return the type of string.

console.log(typeof quantity);

Screen-Shot-2022-05-01-at-9.50.17-AM

We can convert quantity into a number using the Number function like this:

Number(quantity)

We can check that it is now a number by using the typeof operator again.

console.log(typeof Number(quantity));

Screen-Shot-2022-05-01-at-9.57.35-AM

If you tried to pass in a value that cannot be converted into a number, then the return value would be NaN (Not a Number).

console.log(Number("awesome"));

Screen-Shot-2022-05-01-at-10.00.34-AM

How to convert a string to a number in JavaScript using the parseInt() function

Another way to convert a string into a number is to use the parseInt() function. This function takes in a string and an optional radix.

A radix is a number between 2 and 36 which represents the base in a numeral system. For example, a radix of 2 would represent the binary system, while a radix of 10 represents the decimal system.

We can use the quantity variable from earlier to convert that string into a number.

const quantity = "12";

console.log(parseInt(quantity, 10));

What happens if I try to change the quantity variable to "12.99"? Will the result using parseInt() be the number 12.99?

const quantity = "12.99";

console.log(parseInt(quantity, 10));

Screen-Shot-2022-05-01-at-10.45.08-AM

As you can see, the result is a rounded integer. If you wanted to return a floating point number, then you would need to use parseFloat().

How to convert a string to a number in JavaScript using the parseFloat() function

The parseFloat() function will take in a value and return a floating point number. Examples of floating point numbers would be 12.99 or 3.14.

If we modify our example from earlier to use parseFloat(), then the result would be the floating point number of 12.99.

const quantity = "12.99";

console.log(parseFloat(quantity));

Screen-Shot-2022-05-01-at-10.55.03-AM

If you have leading or trailing whitespace in your string, then parseFloat() will still convert that string into a floating point number.

const quantity = "   12.99    ";

console.log(parseFloat(quantity));

Screen-Shot-2022-05-01-at-11.05.35-AM

If the first character in your string cannot be converted into number, then parseFloat() will return NaN.

const quantity = "F12.99";

console.log(parseFloat(quantity));

Screen-Shot-2022-05-01-at-11.08.33-AM

How to convert a string to a number in JavaScript using the unary plus operator (+)

The unary plus operator (+) will convert a string into a number. The operator will go before the operand.

const quantity = "12";

console.log(+quantity);

Screen-Shot-2022-05-01-at-11.14.51-AM

We can also use the unary plus operator (+) to convert a string into a floating point number.

const quantity = "12.99";

console.log(+quantity);

Screen-Shot-2022-05-01-at-11.16.38-AM

If the string value cannot be converted into a number then the result will be NaN.

const quantity = "awesome";

console.log(+quantity);

Screen-Shot-2022-05-01-at-11.18.10-AM

How to convert a string to a number in JavaScript by multiplying the string by the number 1

Another way to convert a string into a number is to use a basic math operation. You can multiply the string value by 1 and it will return a number.

const quantity = "12";

console.log(quantity * 1);

Screen-Shot-2022-05-01-at-11.42.58-AM

As you can see, when we multiply the quantity value by 1, then it returns the number 12. But how does this work?

In this example, JavaScript is converting our string value to a number and then performing that mathematical operation.  If the string cannot be converted to a number, then the mathematical operation will not work and it will return NaN.

const quantity = "awesome";

console.log(quantity * 1);

Screen-Shot-2022-05-01-at-11.18.10-AM

This method will also work for floating point numbers.

const quantity = "10.5";

console.log(quantity * 1);

Screen-Shot-2022-05-01-at-11.56.19-AM

How to convert a string to a number in JavaScript by dividing the string by the number 1

Instead of multiplying by 1, you can also divide the string by 1. JavaScript is converting our string value to a number and then performing that mathematical operation.

const quantity = "10.5";

console.log(quantity / 1);

Screen-Shot-2022-05-01-at-12.08.37-PM

How to convert a string to a number in JavaScript by subtracting the number 0 from the string

Another method would be to subtract 0 from the string. Like before, JavaScript is converting our string value to a number and then performing that mathematical operation.

const quantity = "19";

console.log(quantity - 0);

Screen-Shot-2022-05-01-at-12.11.59-PM

How to convert a string to a number in JavaScript using the bitwise NOT operator (~)

The bitwise NOT operator (~) will invert the bits of an operand and convert that value to a 32-bit signed integer. A 32-bit signed integer is a value that represents an integer in 32 bits (or 4 bytes).

If we use one bitwise NOT operator (~)  on a number, then it will perform this operation: -(x + 1)

console.log(~19);

Screen-Shot-2022-05-01-at-12.20.18-PM

But if we use two bitwise NOT operators (~), then it will convert our string to a number.

const quantity = "19";

console.log(~~quantity);

Screen-Shot-2022-05-01-at-12.28.16-PM

This method will not work for floating point numbers because the result would be an integer.

const quantity = "19.99";

console.log(~~quantity);

Screen-Shot-2022-05-01-at-12.31.16-PM

If you tried to use this method on non-numeric characters, then the result would be zero.

const quantity = "awesome";

console.log(~~quantity);

Screen-Shot-2022-05-01-at-12.32.45-PM

This method does have its limitations because it will start to break for values that are considered too large. It is important to make sure that your number is between the values of a signed 32 bit integer.

const quantity = "2700000000";

console.log(~~quantity);

Screen-Shot-2022-05-01-at-12.36.16-PM

To learn more about the bitwise NOT operator (~) , please read up in the documentation.

How to convert a string to a number in JavaScript using the Math.floor() function

Another way to convert a string to a number is to use the Math.floor() function. This function will round the number down to the nearest integer.

const quantity = "13.4";

console.log(Math.floor(quantity));

Screen-Shot-2022-05-01-at-12.44.53-PM

Just like in earlier examples, if we tried to use non-numeric characters, then the result would be NaN.

const quantity = "awesome";

console.log(Math.floor(quantity));

Screen-Shot-2022-05-01-at-12.46.08-PM

How to convert a string to a number in JavaScript using the Math.ceil() function

The Math.ceil() function will round a number up to the nearest integer.

const quantity = "7.18";

console.log(Math.ceil(quantity));

Screen-Shot-2022-05-01-at-12.48.15-PM

How to convert a string to a number in JavaScript using the Math.round() function

The Math.round() function will round the number to the nearest integer.

If I have the value of 6.3, then Math.round() will return 6.

const quantity = "6.3";

console.log(Math.round(quantity));

Screen-Shot-2022-05-01-at-12.50.20-PM

But if I changed that value to 6.5, then Math.round() will return 7.

const quantity = "6.5";

console.log(Math.round(quantity));

Screen-Shot-2022-05-01-at-12.51.35-PM

Conclusion

In this article, I showed you 11 ways to convert a string to a number using JavaScript.

Here are the 11 different methods discussed in the article.

  1. using the Number() function
  2. using the parseInt() function
  3. using the parseFloat() function
  4. using the unary plus operator (+)
  5. multiplying the string by the number 1
  6. dividing the string by the number 1
  7. subtracting the number 0 from the string
  8. using the bitwise NOT operator (~)
  9. using the Math.floor() function
  10. using the Math.ceil() function
  11. using the Math.round() function

I hope you enjoyed this article and best of luck on your JavaScript journey.
 

Original article source at https://www.freecodecamp.org

#javascript #js #programming 

What is GEEK

Buddha Community

11 Ways to Convert a String to Number in JavaScript
Lowa Alice

Lowa Alice

1624399200

JavaScript Strings Tutorial

JavaScript Strings

📺 The video in this post was made by Programming with Mosh
The origin of the article: https://www.youtube.com/watch?v=09BwruU4kiY&list=PLTjRvDozrdlxEIuOBZkMAK5uiqp8rHUax&index=6
🔥 If you’re a beginner. I believe the article below will be useful to you ☞ What You Should Know Before Investing in Cryptocurrency - For Beginner
⭐ ⭐ ⭐The project is of interest to the community. Join to Get free ‘GEEK coin’ (GEEKCASH coin)!
☞ **-----CLICK HERE-----**⭐ ⭐ ⭐
Thanks for visiting and watching! Please don’t forget to leave a like, comment and share!

#javascript #strings #javascript strings #javascript strings tutorial

Madilyn  Kihn

Madilyn Kihn

1590478483

Convert String To Array Using Javascript Split Method

The easiest approach to use javascript built-in method String.split().

#javascript #javascript string #string to array #morioh #array

Joseph  Murray

Joseph Murray

1624005780

Java: Check if String is a Number

Introduction

Strings are a handy way of getting information across and getting input from a user.

In this article, we will go through a couple of ways check if a String is Numeric in Java - that is to say, if a String represents a numeric value.

Check if String is Numeric with Core Java

Check if String is Numeric with Apache Commons

Check if String is Numeric with Regex

#java #apache commons #java: check if string is a number #check if string is a number #string

Rahul Jangid

1622207074

What is JavaScript - Stackfindover - Blog

Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.

Who invented JavaScript?

JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.

What is JavaScript?

JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.

Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.

JavaScript Hello World Program

In JavaScript, ‘document.write‘ is used to represent a string on a browser.

<script type="text/javascript">
	document.write("Hello World!");
</script>

How to comment JavaScript code?

  • For single line comment in JavaScript we have to use // (double slashes)
  • For multiple line comments we have to use / * – – * /
<script type="text/javascript">

//single line comment

/* document.write("Hello"); */

</script>

Advantages and Disadvantages of JavaScript

#javascript #javascript code #javascript hello world #what is javascript #who invented javascript

Hire Dedicated JavaScript Developers -Hire JavaScript Developers

It is said that a digital resource a business has must be interactive in nature, so the website or the business app should be interactive. How do you make the app interactive? With the use of JavaScript.

Does your business need an interactive website or app?

Hire Dedicated JavaScript Developer from WebClues Infotech as the developer we offer is highly skilled and expert in what they do. Our developers are collaborative in nature and work with complete transparency with the customers.

The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.

Get your business app with JavaScript

For more inquiry click here https://bit.ly/31eZyDZ

Book Free Interview: https://bit.ly/3dDShFg

#hire dedicated javascript developers #hire javascript developers #top javascript developers for hire #hire javascript developer #hire a freelancer for javascript developer #hire the best javascript developers