Data Types: Number
Open the project folder
In our starter files, open the index.html page from this lessons folder:
01.JavaScript Basics > 6.Data Types: Numbers
JavaScript numbers
And we can also change the data type of a variable when re-assigning a value. This is unlike some other languages such as Java, where we need to specify what type of data our variable will contain first. Java variables are declared like this:
String number = "five"
And languages like this are called strongly typed, since the type must be declared first. JavaScript does not care about the type of data it contains.
To better understant this, add the following to our starter project index.html file inside the script section:
let number = "five"
number = 5
document.getElementById('title').innerText = number
- Create a variable using
let. - Then re-assign the number variable to be
5. - Display the value of
number
Here we have the initial number variable assigned a string value. But we then re-assign it with a number. Try this in the browser and you will see the variable has been updated even though the data type is different.
When setting a number notice how we don’t surround it in the quotes. Numbers in JavaScript can be wrote with or without decimals (eg. 5.07, 7, 1.3).
If coming from a different language where there may be multiple number types such as double, float, integer, this is not the case here, JavaScript numbers are always stored in a double precision floating point format.
JavaScript can also calculate a value before storing into a variable:
let number = "five"
number = 5 + 10
document.getElementById('title').innerText = number
Which will result in 15.
Combining numbers and strings
But what will happen if we add a number and a string? Looking at this example:
let number1 = "five"
let number2 = 5 + 10
document.getElementById('title').innerText = number1 + number2
The result will be five15. But is this a number or a string? When adding together a number and a string, the result is a string.
The typeof operator
We can prove this by using an operator called typeof, passing in the value we want to check:
document.getElementById('title').innerText = typeof(number1 + number2)
This results in a value of string. Typeof can be used to see the type of any variable or data, so it is useful if you are unsure. And also bear in mind that even if we have a number stored in quotes, this is still classed as a string too. And you can see with this example:
let number1 = "5"
let number2 = 5 + 10
document.getElementById('title').innerText = typeof(number1 + number2)
Again an output of string will occur.
Primitive types
Both string and number are considered to be JavaScript primitive types. Primitives are data types that are not an object, they also don’t have any methods, which are actions that can be performed.
As an example, an object which is not a primative type, has methods to allow us to perform actions such as selecting the values, freezing the data so it cannot be changed, and creating an array from the contents.
Whereas these primitives such as number and string, are simple in comparison and we cannot perform any actions. We will have more on this in a later section.