Nerd For Tech
Published in

Nerd For Tech

Let’s See together What is Data Types in JavaScript and Why it’s Different from Other Languages?

Photo by Irvan Smith on Unsplash

All programming languages have some kind of data type available, and they are different from one language to another language. These data types help us to understand the type of data any variable contains.

Good to Know

As we know JavaScript is a loosely typed and dynamic language. What I mean if we declare any variable the type of variable gets defined as per the value, we have assigned to it.

let dynamicTyped = "Foo"; // string typedynamicTyped = 100; // number typedynamicTyped = false; // boolean typedynamicTyped = {}; // object type

As we see in the above example, if we are changing the variable's value, it’s changing its type also.

What is Data type?

Data types help us to understand the type of data any variable holding and accordingly we can play with its value.

In JavaScript, we have the following data types available.

  1. Primitive Data Types
  2. Structural Data Type (Non-Primitive Data Types)

Primitive Data Types

Primitive Data types are those data types that hold a single value and immutable. There are 7 primitive data types: string, number, bigint, boolean, undefined, symbol, and null.

A mutable object is an object whose state can be modified after it is created.
Immutable is the object whose state cannot be changed once the object is created.

Below is the list of primitive data types -

  1. undefined: A variable that is declared but value not assigned yet it’s undefined.
  2. boolean; Boolean can only have true or false values as per the need.
  3. string: String type contains any value in textual format.
  4. number: number type can have data integer values.
  5. bigint: to represent whole numbers larger than 2⁵³ -1.
  6. symbol: This data type can be referred to as a “String value”.
  7. null: null is an exceptional kind of data type. It accepts a single value but if we do typeof null then it will return “object”. In computer science, a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address
const undefinedType;                          // typeof undefinedconst stringType = "Foo";                     // typeof stringconst numberType = 12345;                     // typeof numberconst booleanType = true;                     // typeof booleanconst bigIntType = BigInt(9007199254740991n); // typeof bigintconst symbolType = Symbol("Symbol");          // typeof symbolconst nullType = null                         // typeof object

Structural Data Type (Non-Primitive Data Types)

The structural data type is the data type used to create the structural format of data. Like: new Object(), new Array(), new Map(), new Set() are the structural data type. Functions are also known as the first-class citizen in JavaScript and it also falls in this category.

  1. Object: Anything that uses a new keyword to instantiate is the type of object.
  2. Function: A function is a code snippet that can be called by other code or by itself or a variable that refers to the function
const objectVar = new Object(); // typeof objectconst objectLitteral = {};      // typeof objectconst arrayVar = new Array();   // typeof objectconst arrayLitteral = [];       // typeof objectconst setVar = new Set();       // typeof objectconst mapVar = new Map();       // typeof object

In the above examples, we saw different ways to define object type data, and for each of one if we do typeof then it’s returning the object. But what if we need to check Array, Set and Map type explicitly. Let see the below examples.

// different ways to implicitly check array type
Array.isArray(arrayVar) // true
Array.isArray(arrayLitteral) // true
arrayVar.constructor === Array; // truearrayVar instanceof Array; // true// Checking set type explicitly
const setVar = new Set();
setVar instanceof Set; // true
// Checking map type explicitly
const mapVar = new Map();
mapVar instanceof Map; // true

Hope this will be helpful for you. Happy Learning! :)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store