There are five data types in JavaScript, and they are:
Strings
In my previous posts on JS Basics, I discussed strings. Strings are data, embedded inside quotation marks.
fridge('sweets');
Numbers
In JavaScript, numbers can be assigned to strings.
var myNumber = 12;
There is a difference between assigning a number to a variable, and assigning a stringed number to a variable because they give different results in the console.
var myNumber = 12;
var myString = "12";
Booleans
In JavaScript, a boolean can only have 2 values: true
or false
. Booleans are mostly used when working with conditionals.
if (myNumber === 12) {
console.log(true)
}
Undefined
undefined
in JavaScript means an unassigned variable (variable without a value).
let z
console.log(z); //the result is undefined
Null
null
in JavaScript means a variable with no value existing. You can use null
to empty an object
person = null; //null is defined as the value
Differences
null | undefined |
Defined, but without a value | Don't exist |
Summary
There are 5 data types:
- Strings
- Numbers
- Booleans
undefined
null
At first, they can be difficult to understand, especially undefined
and null
. But, with constant practice, coding JavaScript with them will be a piece of cake.