However, these data structures often vary in each language.
Here's a brief overview of the seven primtive types:
The Number type is used when you need to carry out some mathematics. It’s a double-precision 64-bit binary format IEEE 754 value, but you don’t need to worry about that for now!
3 1946 0.5
When you need to work with characters, words or sentences, or print something on the screen, you’ll need to use the String type.
‘Mario’ “apple" ‘What a wonderful sentence this is.'
Booleans are really helpful when you need your program to make a decision.
The Boolean type has just two values:
The Null type has just one value:
It represents a lack of identification i.e. if a variable is null, it doesn’t point to any object.
The Undefined type is used to represent a variable that has not been assigned a value.
The BigInt type can be used when the Number type just doesn’t cut it. It’s for huge numbers. Massive numbers.
Similar to how SQL uses FLOAT and DOUBLE, BigInt can be used to represent larger values at the cost of precision.
It’s really important to note that null and undefined are NOT the same. Null is often assigned intentionally as a representation of no value.
The seven primitive types define immutable values known as ‘primitive values’. More on this below.
So what’s a value and what’s a type? It can get a little blurry and it’s pretty abstract, but it helps to know these fundamentals when building and debugging applications later down the road.
Essentially, when writing a program you’ll need to express values. Depending on what you need to do with these values, you’ll need to choose different representations or types for them.
So, in programming lingo, types simply refer to how values are represented.
What the heck does ‘immutable’ mean?! Don’t panic.
If something is immutable, it simply means that its state can’t be changed after it’s created. That’s it. Therefore, if something is ‘mutable’, it can be changed.
So immutable means unchangeable and mutable means changeable. Got it?
Primitive types therefore represent data that it is not an object and has no methods.
You may have heard the term ‘literal’ used in reference to values. A literal represents a fixed value in source code.
In other words, if you include a value directly in your program code, you’re using a literal.
The easiest way to think about literals is that you’re literally writing the value in your code.
Let’s look at some examples.
We could declare a string literal like so:
var name = ‘Mario’;
String literals need to be enclosed in single (‘…’) or double (“…”) quotes. You can use either one, but choose one and stick with it throughout your program for consistency. The choice becomes more critical when you’re manipulating strings or working with JSON.
boolean values can also be written as literals directly within the source code:
var age = 30; var bool = false;
Handily, it will return a string indicating what type you’re dealing with:
console.log(typeof ‘Mario’); // string console.log(typeof 30); // number console.log(typeof false); // boolean
Consider the following:
var a = 57; // a is a number a = ‘Luigi’; // a is now a string a = true; // a is now a boolean
a is first assigned as a
number, then re-assigned as a
string, then to a
Dynamic and static typing each have their associated advantages and disadvantages. Understanding that JS is loosely typed is important in order to avoid issues and surprises later on.
This can be particularly confusing when it comes to type coercion, which I’ll break down in another article.