Turn Numeric Text Into Numbers

Twice already in this course, text that looked like a number got in your way. In Compare Numbers and Text With Boundaries, "10" < "9" came back true, because JavaScript compared two strings character by character. In Ask Whether Two Values Are the Same, a booking form’s "3" didn’t match the number 3.

Numbers often reach a program as text. A trail app might export your gear list with weights like "2.5", and whatever someone types into a form field arrives as text, too. Before you can total those weights or compare them with a limit, you need real numbers.

In this lesson, you’ll learn to check what type a value has, turn numeric text into a number, notice when the text wasn’t a number at all, and turn a number back into text.

Ask a Value for Its Type With typeof

So far, you’ve told text and numbers apart by looking for quotes. JavaScript can also tell you directly. The typeof operator goes in front of a value and gives back the name of that value’s type as a string.

Start the Node REPL by typing node in the integrated terminal. Predict each answer before you press Enter:

Language: JavaScript
> typeof 2.5
'number'
> typeof "2.5"
'string'
> typeof true
'boolean'
> typeof undefined
'undefined'

The first two lines show the difference that tripped up "10" < "9". The value 2.5 is a number, while "2.5" is a string that happens to contain digits. The last line shows that undefined, which the REPL prints after each const line, has a type of its own.

Notice the quotes around every answer. typeof gives back text, such as 'number', which you can store or compare like any other string.

Turn Text Into a Number With Number()

To convert text into a number, you pass it to Number(). It reads the whole text, and when all of it spells a number, it gives back that number. You call it like console.log(), with the value inside the parentheses. Try it in the REPL:

Language: JavaScript
> Number("2.5")
2.5
> typeof Number("2.5")
'number'
> Number("10") < Number("9")
false
> Number("3") === 3
true

The result 2.5 has no quotes, so it’s a real number now. With the text converted to numbers, both earlier comparisons behave as you’d expect. Ten isn’t less than nine, and the converted "3" strictly equals 3.

Number() doesn’t change the text you give it. It produces a new value, which you need to store if you want to use it again. A good habit is to convert each piece of text once, right where it enters your program, and give the number its own name.

To see that habit in a script, leave the REPL first by typing .exit and pressing Enter. Then create a file named gear.js in your scratch/ folder:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.