Data Types in JavaScript

divyanshchahar

Divyansh Chahar

Posted on August 27, 2022

Data Types in JavaScript

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);
Enter fullscreen mode Exit fullscreen mode
number
string
Enter fullscreen mode Exit fullscreen mode

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:

  1. Boolean
  2. Null
  3. Undefined
  4. Number
  5. BigInt
  6. String
  7. 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);
Enter fullscreen mode Exit fullscreen mode

The above code will give the following output

string
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

The above code will result in the following output

boolean
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

The output of the above code will be as follows

string
string
Enter fullscreen mode Exit fullscreen mode

Undefined

A variable that has not been assigned a value is of type undefined.

var w;
console.log(typeof w);
Enter fullscreen mode Exit fullscreen mode
undefined
Enter fullscreen mode Exit fullscreen mode

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 a typeof operator it will return object as output.

const a = null;
console.log(typeof a);
Enter fullscreen mode Exit fullscreen mode

The output of the above code is given below

object
Enter fullscreen mode Exit fullscreen mode

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 21024-2^{1024} to 210242^{1024} . All the floating point number greater then 21074-2^{-1074} and less then 210742^{-1074} are converted to 00 . All the negative floating point integers less then 21024-2^{1024} are converted to - \infty and all the numbers greater then 210242^{1024} are converted to ++ \infty .

Also it must be noted that numbers data type can store integers in the range of (2531)-(2^{53} -1) and 25312^{53} -1 .

Image description

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 store
  • Number.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
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

when the above code is run the output will be the following

bigint
number
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
divyanshchahar
Divyansh Chahar

Posted on August 27, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related