Understanding variables and Data Types in JavaScript

Variables in JavaScript:-
Variables in JavaScript are named containers where we can store values and use those values for programming purposes in the future. We can compare a variable to an empty vessel where we can store water for our future needs. We can declare variables in JavaScript in three way they are :-
i)Var:- In JavaScript if we declare a variable with var then that are functional scope, meaning they are scoped to the function in which they are defined. They do not respect block scope (such as within an if, for, or while block) and are accessible outside those blocks. The variable declared with var keyword can be redeclared as well as can be reassign.
Note:- Using var keyword to declare a variable is not recommended in modern JavaScript.
ii)Let:- In JavaScript if we declare a variable with let then that variable are block scope. Variable declared with let can be reassigned but cannot be redeclared. If we declare a variable inside a block scope --> {} then that variable is not accessible outside that particular block.
iii)Const:- If we declare a variable with const then that variable are block scope. Variable declared with const cannot be reassigned nor be redeclared. But when we declare an object with const in JavaScript it establishes a constant variable reference. For objects, this means the variable cannot be reassigned to a different object, but it does not prevent modifications to the object's properties. The const reference maintains a fixed memory address where the object is stored. While this address remains constant, the values of the object's properties at that address are mutable. Therefore, const provides reference immutability, ensuring the variable always points to the same object, but it does not enforce immutability of the object's internal properties.
JavaScript Variable Declaration Visually representation
2.DataTypes in JavaScript.
In JavaScript Data types are divided into two parts :- i) Primitive ii) Non-Primitive.
Further Primitive Data type are divided into 7 types:-
String--> let name = "Ram"; ---> The value Ram is a string we can declare a string inside both single quote and double quote.
Number--> let age = 30; --> The value of age is a number.
Boolean--> True/ False; --> "Is the sky blue?" - "true" (because the sky is typically blue), and "Is the sun green?" - "false" (because the sun is not green);
Undefined--> It means that the variable is declared but no value is assigned to it.--> let name;--> Here name variable is declared but no value is assigned to it.
Null--> Null in JavaScript is a stand alone value, Null means nothing or empty. Though Null falls under Primitive Data type but when we check the typeof [an operator which tell the data type] of Null in JavaScript it returns us object and it is the fault of the JavaScript language and when Javascript team get realised that many code bases had already been written by using this value Null.
BigInt--> BigInt Handles very large integers beyond standard number limits. Created with n suffix (e.g., 123n).
Symbol--> Symbols in JavaScript are unique, immutable primitive values primarily used as object property keys to avoid naming collisions. Each Symbol created is distinct, even with identical descriptions. They are not enumerable in standard loops, providing a way to create hidden properties.
Further Non-Primitive Data type are divided into 3 types:-
Array--> An array in JavaScript is a versatile, it can store different Primitive Data types in a single array. An Array store its value in contiguous memory location. The indexing of an Array starts from 0[zero] where the 1st element in the resides at index 0. --> let arr = ["Ramesh", 25, true, undefined, null];
The value of arr[0] = "Ramesh";
2)Object--> Objects in JavaScript is a collection of key-value pairs where the keys are typically String and values can be any data types in JavaScript. Objects are used to represents the real life entities or concepts through their properties (keys) and associated values. In JavaScript Objects are the fundamental building blocks it helps us to create complex data structures and the implementation of object-oriented programming paradigms.
Example:- let obj = {
name:"Ram",
age:25,
address:{
city:"Kolkata",
state:"West Bengal",
}
}
3) Functions--> Functions in JavaScript is considered as a first class citizen. In JavaScript, a function is a block of reusable code designed to perform a specific task. It encapsulates a sequence of statements that can be executed multiple times, potentially with different inputs (parameters), and can return a value. In JavaScript we can pass a function as a parameter inside another function. In Javascript Functions are essential for organising code, promoting modularity, and avoiding redundancy. Functions enable us to create reusable logic, break down complex problems into smaller, manageable parts, and enhance code readability.
Conclusion
In this blog, we discussed variables and the different data types in JavaScript, illustrating them with various examples and visual explanations. In future blogs, I will delve into the different types of memory where these data types are stored. Understanding these memory structures will empower us to write more efficient and robust code.
Subscribe to my newsletter
Read articles from prasenjit basu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

prasenjit basu
prasenjit basu
Hi, I am Prasenjit Basu Roy I am a Web-Developer.