Datatypes in JavaScript

Datatypes in JavaScript

In Javascript, the datatype specifies which type of data a language can store and process. There are two types of Datatypes in JavaScript

·

5 min read

Datatypes are the foundational topic of any programming language. Today we will learn about Datatypes in JavaScript. Datatype specifies which type of data a language can store and process.

There are two types of Datatypes in JavaScript:

  1. Primitive

  2. Non-Primitive (Reference Types)

This categorization is made based on two parameters:

  1. How we store value in memory

  2. How we access the value from memory

Primitive Datatypes in JavaScript

Primitive types are Pass by Value. This means that copies of their value are used when passed to functions or assigned to other variables.

This behaviour is called Pass by Value because you are passing a copy of the value to the function. Primitive types (e.g., numbers, strings, booleans) are immutable in JavaScript, this means their values cannot be changed after creation. These values are like locked boxes – you can't change what's inside. If you modify the value inside a function, it won't change the original value outside the function because you're working with a separate copy. When you modify a primitive type inside a function, you're working with a new copy of the value, leaving the original value unaffected.

There are seven primitive datatypes are there in JavaScript.

  1. Number: Represents numeric values, both integers and floating-point numbers.

  2. String: Represents textual data enclosed in single or double quotes.

  3. Boolean: Represents a true or false value.

  4. Undefined: Represents a variable that has been declared but hasn't been assigned a value.

  5. Null: Represents an intentional absence of any value or object.

  6. Symbol (introduced in ECMAScript 6): Represents a unique and immutable value, often used as object property keys.

  7. BigInt (introduced in ECMAScript 11): Represents large integers that cannot be represented as regular JavaScript numbers.

Non-Primitive Datatypes in JavaScript

In JavaScript, Non-Primitive types are Passed by Sharing a Reference. This means changes made to copies will affect the original value since they share the same reference.

This means when you pass an object or an array into a function, you're passing a reference to the same object or array in memory. If you modify the object or array inside the function, those changes will affect the original object or array outside the function because you're working with the same underlying data.

It's important to understand that while it may seem like pass by reference, JavaScript is technically using pass by sharing a reference for these non-primitive types. This is a subtle but important distinction because you're not passing the actual object; you're sharing a reference to the object's location in memory.

There are mainly three Non-Primitive Datatypes in JavaScript:

  • Object: Represents complex data structures and collections of key-value pairs. Objects can be created with curly braces {} or with constructors like new Object().

  • Array: A special type of object that represents a list of values, indexed by integers, and is created using square brackets [].

  • Function: A type that represents executable code and is used for defining reusable functions. Functions are first-class citizens in JavaScript.

  • Date: Represents date and time information.

  • RegExp: Represents regular expressions used for pattern matching within strings.

  • Error: Represents errors thrown during runtime.

Return Type of Variables in JavaScript

Below are the Return Types of Every Variables in JavaScript. Remember them as they will help you in Interviews and in Programming as well. You can check the Return Type of Variables using typeof operator. Below is a small example:

let num = 42;
let str = "Hello";
let obj = { key: "value" };

console.log(typeof num); // "number"
console.log(typeof str); // "string"
console.log(typeof obj); // "object"

Primitive Datatypes

  1. Number -> number

  2. String -> string

  3. Boolean -> boolean

  4. null -> object

  5. undefined -> undefined

  6. Symbol -> symbol

  7. BigInt -> bigint

Non-Primitive Datatypes

  1. Array -> object

  2. Function -> function

  3. Object -> object

Conclusion

JavaScript has two main categories of datatypes: Primitive and Non-Primitive (Reference Types).

Primitive Datatypes:

  1. Number: For numeric values, including integers and floating-point numbers.

  2. String: For textual data enclosed in single or double quotes.

  3. Boolean: For true or false values.

  4. Undefined: For declared but unassigned variables.

  5. Null: For representing the intentional absence of a value or object.

  6. Symbol (introduced in ECMAScript 6): Represents unique and immutable values, often used as object property keys.

  7. BigInt (introduced in ECMAScript 11): Handles large integers that can't be represented as regular JavaScript numbers.

Primitive datatypes are passed by value, making copies of their values, and are immutable.

Non-Primitive Datatypes:

  1. Object: Used for complex data structures and collections of key-value pairs. Created with curly braces or constructors.

  2. Array: A special type of object representing lists of values, indexed by integers, created using square brackets.

  3. Function: Represents executable code and defines reusable functions. Functions are first-class citizens in JavaScript.

  4. Date: Handles date and time information.

  5. RegExp: Manages regular expressions used for pattern matching within strings.

  6. Error: Represents errors thrown during runtime.

Non-primitive datatypes are passed by sharing a reference, meaning changes made to copies will affect the original value, as they share the same reference to the object's location in memory.

💡
This is the second post in our React Series. Subscribe and Follow for more such content. Jai Hind, Vande Mataram 🇮🇳

Read more about React & JavaScript

Follow me for more such content.

Did you find this article valuable?

Support Jay Tillu by becoming a sponsor. Any amount is appreciated!