Data Types in JavaScript
Divyansh Chahar
Posted on August 27, 2022
As the name suggests in this piece we are going to discuss different data types in JavaScript.
JavaScript is a loosely typed language i.e. variables can be declared anywhere for example within a loop or conditional statements. It must also be noted that variables are dynamic in nature i.e. the type of data stored in them can also be changed. Please look at the code below
var a = 1;
console.log(typeof a);
a = "a";
console.log(typeof a);
number
string
Before we take a deep dive into different data types it is important to mention that in this article we are only going to discuss primitive data types i.e. data types which are building blocks of other data types (ex: Objects).
In JavaScript there are seven types of primitive data types:
- Boolean
- Null
- Undefined
- Number
- BigInt
- String
- Symbol
Before proceeding further it is important to know about the typeof
operator. Data type of a variable can be checked by using the typeof
operator as shown in the code below
let name = "javascript";
console.log(typeof name);
The above code will give the following output
string
Boolean
Boolean type variables can only store boolean values i.e. true
and false
. See the code below for better understanding.
var x = true;
console.log(typeof x);
The above code will result in the following output
boolean
String
String type variables store textual data for example, a single character or a complete sentence.
var w = "word";
var s = "This is a complete sentence";
console.log(typeof w);
console.log(typeof s);
The output of the above code will be as follows
string
string
Undefined
A variable that has not been assigned a value is of type undefined.
var w;
console.log(typeof w);
undefined
Null
A variable is of type null if it has been intentionally left blank.
There is difference between null and undefined. A variable is of type null if its value has been intentionally left blank where as variable is of type undefined if its value has not yet been defined.
Null datatype cannot be identified using
typeof
operator. If a variable containing a null value is passed to atypeof
operator it will return object as output.
const a = null;
console.log(typeof a);
The output of the above code is given below
object
Symbol
To understand symbols you will need better understanding of the concepts of immutability in Javascript. I will explain this concept in more details in a follow up article for the time being you just need to understand that symbol data types are immutable and unique.
Number
Number type variables can store double precision floating point numbers in the range of to . All the floating point number greater then and less then are converted to . All the negative floating point integers less then are converted to and all the numbers greater then are converted to .
Also it must be noted that numbers data type can store integers in the range of and .
If you want to check which if a value can be stored as a number data type, the following refrences could be useful:
Number.MAX_SAFE_INTEGER
is the the largest integer value that number can storeNumber.MIN_SAFE_INTEGER
is the smallest integer that number can store.Number.MAX_VALUE
is the largest positive floating point value that number can store.Number.MIN_VALUE
is the smallest floating point value that number can store.- Number.MIN_VALUE
is the largest negative floating point value that number can store.- Number.MAX_VALUE
is the smallest negative floating point value that number can store.
console.log(Number.MAX_SAFE_INTEGER); // largest integer that can be stored in number => 9007199254740991
console.log(Number.MIN_SAFE_INTEGER); // smallest integer that can be stored in number => -9007199254740991
console.log(Number.MAX_VALUE); // largest positive floating point number that be stored in number => 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // smallest positive floating point number that be stored in number => 5e-324
console.log(Number.MIN_VALUE * -1); // largest negative floating point number that be stored in number => -5e-324
console.log(Number.MAX_VALUE * -1); // smallest negative floating point number that be stored in number => -1.7976931348623157e+308
BigInt
BigInt is used to used to store integers which are outside the limit of number data type. Any integer can be converted to a bigInt by using the suffix n
. See code below for better understanding.
const a = 1n;
const b = 1;
console.log(typeof a);
console.log(typeof b);
when the above code is run the output will be the following
bigint
number
Posted on August 27, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.