Introduction
Let's say you got a temporary job as a secretary while learning to be a software developer. Your daily job includes getting the information of all individuals who visit your boss. The information might include their names, their purpose of visit and their contact.
You cannot memorize all the information you get from all visitors. You could probably store them in a book or on your computer so that you can easily retrieve them whenever you want to. And when you store them you would give each piece of information its label, like "name" for the visitor's name
Javascript Data
The information you store as a secretary can be likened to data in Javascript. Javascript data have different labels and these labels represent the different formats of storing data in Javascript.
The 8 data types in Javascript
There are 8 different ways of storing data in javascript, and they are:
String
Number
Boolean
Object
Null
Undefined
Symbol
BigInt
String
The string type represents textual data, A string is surrounded by a single quote or a double quote. Each element in a string occupies a certain position in the string. The first element is at index 0, and the second and third are at index 1 and 2 respectively.
In the example below, I declared two separate variables and assigned them to string values.
let name = 'James';
let newName = "Peter";
Number
The number types are stored in 64-bit format also known as double-precision floating point numbers. They consist of numbers that are within the range of minus two raised to the power of fifty-three minus 1 and two raised to the power of fifty-three minus 1, with both of these numbers inclusive. These two numbers are known as the MIN_SAFE_INTEGER and the MAX_SAFE_INTEGER respectively.
Numbers that exceed this range, belong to another data type called BigInt.
In the example below I declared separate variables and assigned them to a positive integer, a float and a negative integer value respectively.
let number = 12;
let anotherNumber = 1.2;
let lastNumber = -4;
Boolean
The boolean type is logical, and it has only two values; true or false. It is commonly used in loops and conditional statements.
In the example below, I declared variables and assigned them a value of true and false respectively. Then, I created a conditional statement that returns 1 if the "bool" variable is true or -1 if it is false and returns zero if it is neither true nor false.
let positive = true;
let negative = false;
let bool = false;
// conditional statement
if (bool) {
return 1;
} else if (bool) {
return -1;
} else {
return 0;
}
Object
An object type allows you to store collections of data. The data are stored in a pair of curly braces and a key-value pair format. Object keys must be textual (i.e string).
An object can store any of the other data types, like string, number, arrays, booleans and so on.
In the example below we created an object named "myObject", and we gave it three keys with their corresponding values.
let myObject = {
name: Musab,
number: 12,
developer: [true, "David", 1]
}
You must always put a comma at the end of every value in an object except the value is the last in the object.
Null
Null data types are special data types. They have the value null which means that the value is unknown or empty.
let unknown = null;
Undefined
Unlike null, undefined means that a variable is declared and not assigned a value. A variable can also be assigned undefined specifically.
let name = undefined;
let newName;
console.log(newName);
If you log the value of the "newName" variable in the above example, you will get undefined.
Symbol
Symbols are used to create unique values. They are created by calling the Symbol() function. Every time the Symbol() function is invoked it creates a new unique value.
Symbols are kept hidden or private and they can only be used internally. For instance, they can be used as keys in an object, but when you try to get the list of keys in an object where a symbol is part of its keys the symbol key will not be displayed
You can pass a parameter to the symbol function to serve as a description for the symbol when debugging it in the console.
let firstSymbol = Symbol();
let anotherSymbol = Symbol(anotherSymbol);
BigInt
BigInt is a special type of number that provides support for numbers with a length that a normal number type can not hold (i.e numbers that exceed the safe integer limit). It can be created by appending n to the end of an integer or passing the number into the BigInt function.
let bigNumber = 1246835794537363783628239474364746447464n;
let newBigNumber = BigInt(1246835794537363783628239474364746447464n);
let anotherBigNumber = BigInt(12);
To wrap it up
So, those are the eight data types in Javascript. In programming languages like Typescript and Java, you will be required to explicitly state the data type you want the value of your variable to be. But, in Javascript, you cannot do that. You can only deduce the data type of your variable by checking the value assigned to it.
I hope that having read this piece you will better understand the eight data types in Javascript and also take note of and use them effectively in your code. Keep learning, keep growing.