Today's article is going to be a continuation of the series I introduced in my last blog post here (basically I will be writing about different basic javascript concepts, types, and functions but the aim behind this series is to break things down in simple terms so that a non-programmer friend or a newbie can grasp one or two things about coding from me, especially Javascript).
So, today we are going to be diving into the world of data types. As a developer, I use data types often. So it's one topic that is very important to understand. In simple terms, if I'm going to describe what a data type is to a newbie or a non-programmer friend, I would say that "data types tell us what kind of data is being stored in a variable" (wondering what a variable means? you can read about all of that here).
A value in javascript is always of a certain type. As an example, a value can be a number or a string. The type of value that a variable can hold (or store) is referred to as the data type. Data types could be numbers, strings, and boolean, amongst others. The latest ECMAScript standard defines eight data types.
- String
- Number
- Bigint
- Boolean
- Undefined
- Null
- Symbol, and
- Object
Note: Arrays are not part of this list because they are objects
These eight data types can be divided into 2 categories- Primitive data types and, (String, Number, BigInt, Boolean, Undefined, Null, Symbol).
- Non-primitive or reference data types (Object (array functions)).
So what is the difference between primitive and non-primitive data types? As a front-end developer, this is one of the most common interview questions. The simple answer to that is: primitive data types are immutable (there is no way to change their value once they are created. This is different from reassigning a value) while non-primitive data types are mutable (we can change or replace their value after creation).
That being said, let's break down the primitive data types with some examples. The primitive data types are immutable as described above and can be compared by values. They also do not have properties or methods. There are 7 primitive data types and I will be explaining each one of them below.
Primitive data types with examples
» String:- This represents a sequence of characters. It can be defined by using single quotation marks, double quotation marks, or backticks.
» Number:- This represents numbers with or without decimals. It can be defined without quotes or a number wrapper object.
» Bigint:- This is a new data type that was introduced in ES11. It can be used to store integer values that are too big to be represented by a normal JavaScript Number. Any number larger than±(253-1)
(which is the limit for the primitive data type number) falls into this category. Bigint can be created by adding an n at the end of the number or with the wrapper object BigInt (value).
» Boolean:- This is a data type that has only two values (true or false). They are often used for conditional testing.
» Undefined:- When a variable is declared and not assigned then it is undefined. Basically, a variable without a value is undefined. The type is also undefined.
» Null:- This means an empty value. Null is used when you want to indicate that a variable is defined but has no value yet.
» Symbol:- This is new in ECMAScript6. It's a unique and immutable identifier.
Next up is the non-primitive data type. In this category, we have only the object (array functions).
» Objects:- This contains key-value pairs