I was thinking to create a series of the most common Front-end interview questions and finally I have made a list of the questions and this is going to be the first question of that list. Let’s discuss the question without further ado!

According to the latest ECMAScript standard there are 8 types of different data types. Out of which seven data types are Primitive and one is Non-Primitive (also known as reference type).


Primitive 👇

  • Primitive data types are the fundamental data types in JavaScript.
  • They hold a single value and are immutable (cannot be changed once created).
    • Meaning their values cannot be changed after they are created. Any operation that appears to modify a primitive value actually creates a new value.
    let num = 8; // num holds the value 8 (a primitive number)
    num = num + 2; // Adding 2 to num, but not modifying the original value
    console.log(num); // Output: 10
    // The original value of 'num' (8) remains unchanged.
    
  • They directly store the value itself.
  • Examples include: String, Number, Boolean, Undefined, Null, and Symbol.

Non-primitive Data Types (or reference types) 👇

  • Non-primitive data types are more complex and can hold multiple values and methods.

  • They are mutable and are stored as references.

    • Meaning their values can be modified even after creation. Operations performed on these types can directly change their internal state.
    let person = { name: 'Shahzaib', age: 22 }; // person holds a reference to an object in memory
    person.age = 23; // Modifying the 'age' property of the object
    console.log(person); // Output: { name: 'Shahzaib', age: 23 }
    // The object's 'age' property has been directly modified.
    
  • They store a reference to a location in memory where the data is stored.

  • Examples include: Object, which can include arrays, functions, and other objects.

In JavaScript, variables that hold primitive values directly contain the value, while variables that hold non-primitive values contain a reference to the value stored in memory.

Example

  • Primitive data types like const num = 8 store the actual value 8.
  • Non-primitive data types like const obj = { key: 'value' } store a reference to the memory location where the object { key: 'value' } is stored.

Note: Array is not a separate data type in Javascript because it is object as well.

Now you have got a solid idea about Primitive and Non-Primitive. Let’s discuss all of the 8 data types one by one in a quick way.

String

String contains a group of characters inside single or double quotes and it belongs to primitive type.

const str = "Hello world";
const str2 = "justCodingThing.com";

Number

Number represents numeric values e.g 2, 12, 100. Unlike other programming languages, we don’t need int, float, etc to declare different numeric values and it belongs to primitive type.

const num = 12;
const num = 12.25;

BigInt

BigInt represents an integer with arbitrary precision, It allows us to handle values far beyond the limits of a standard JavaScript number and it belongs to primitive type.

const veryBig = 12345678901234567890n;

Boolean

Boolean represents a logical value, either true or false. It belongs to primitive type.

const isLearning = true;
const isEarning = false;

Undefined

When a variable is declared and not initialized or not assigned with any value. By default, the variable was stored with an undefined value and it belongs to primitive type

let total = undefined;
// or
let total;

Null

null is mainly used as a value that is assigned to a variable. Assigning a null to a variable means that the variable contains no value, similar to the meaning of undefined which also means empty value and it belongs to primitive type.

let user = null;

Symbol

This data type was introduced in a newer version of JavaScript. Symbol is an immutable primitive value that is unique.

const first = Symbol("shahzaib");
const second = Symbol("shahzaib");

console.log(first === second); // false

Object

An object is a complex data type that allows us to store key-value pairs of collection of data and it belongs to Non-Primitive (Reference type).

const student = {
  firstName: "Shahzaib",
  lastName: null,
};

That’s all


If you have any feedback, feel free to reach out to me at shahzaibmohammad07@gmail.com.

If you find this helpful please share with your network ✅.

Happy Coding 🙌 !