Unsurprisingly, the number type represents numbers.
NaN(stands for "not a number", but is considered a number)
const favoriteNumber = 42;
You can use certain methods on numbers.
> 3.14159.toFixed(2); "3.14"
As you've seen, the string type represents text.
const greeting = "hello, world!";
You can use certain methods on strings.
> "hello".toUpperCase(); "HELLO" > "hello".charAt(2); "l" > "hello".toUpperCase().charAt(2); "L"
Booleans can only hold 2 possible values -
> 5 > 3; true > 5 > 10; false > "hello".charAt(2) === "e"; false > const enrolledAtEpicodus = true;
You'll discover soon just how important booleans are to programming.
The undefined data type has only one possible value -
When declaring a variable without giving it a value (
Additionally, there are some functions and methods that do not return any value, in which case the return value is actually undefined. (You'll learn more about this when we learn how to write our own methods and functions.)
It's important to understand the difference between the number
5 and the string
"5". To the computer, they are two entirely different things, as illustrated by this example:
> const myNumber = 5; > const myOtherNumber = 10; > const myText = "5"; > const myOtherText = "10"; > myNumber + myOtherNumber; 15 > myText + myOtherText; "510"
When we added
5 we got 15, but when we added
"5" it concatenated the two strings together.
Likewise, the boolean
true is not the same as the string
In the example above, somewhat confusingly, the
+ operator works on both numbers and strings - just differently. Usually methods will only work on a specific data type. For example,
48432.78.toExponential(); works just fine, but trying to do
"48432.78".toExponential(); results in an error because there is no toExponential() method that works on a string. Likewise,
"hello".charAt(2); works, but
314159.charAt(2) does not.
We can check the data type of a variable or value as follows:
> typeof 5; "number" > typeof "5"; "string" > typeof true; "boolean" > typeof "true"; "string" > const greeting = "hello world"; > typeof greeting; "string"
Often input from a web browser will come in as a string and we will need to convert it to a number before working with it.
We can convert a string to a number by passing a string to the parseInt() function (more on functions soon):
> const inputtedAge = "45"; > inputtedAge; "45" > typeof inputtedAge; "string" > const myAge = parseInt(inputtedAge); > myAge; 45 > typeof myAge; "number"
Note that if you try to use parseInt() to convert a string not actually containing a number, the result is the "number"
> const name = "Andrea"; > const nonsense = parseInt(name); > nonsense; NaN
If you need to go the other way around, you can convert a number to a string by calling the toString() method on the number:
> const myNumber = 42; > myNumber; 42 > typeof myNumber; "number" > const convertedNumber = myNumber.toString(); > convertedNumber; "42" > typeof convertedNumber; "string"
Number: Numerical value.
String: Content inside of quotes. Such as
"This is a string!".
Undefined: An object (such as a variable) without a defined value.
Null: For now, know that this represents nothingness. We'll explore this more later.
parseInt(): Converts a string to a number.
toString(): Converts a number to a string.
To check the data type of a variable or value:
> typeof 5; "number"
To convert a string to a number:
> const myNumber = parseInt("42") > myNumber; 42
To convert a number to a string:
> const convertedNumber = 42.toString(); > convertedNumber; "42"
Lesson 17 of 61
Last updated July 30, 2020