Variables and Data Types in JavaScript
In this article we learn about the variables and data types in js. Difference keywords like var, let, and const which we can use with any variable declaration.
What is a Variable?
A variable is like a container used to store information.
Imagine you have a box labeled "Name". Inside that box, you store the value "Mukul".
Later, whenever you need the name, you simply check the box.
In JavaScript, variables work the same way. They store data that your program can use later.
let name = "Mukul";
Variables help us store, reuse, and update information in our programs. Without variables, we would need to write values repeatedly.
Declaring Variables in JavaScript
JavaScript provides three keywords to declare variables: var , let , and const.
var city = "Delhi";
let age = 22;
const country = "India";
// there is some difference between these keywords.
Primitive Data Types
Data types define what type of data a variable holds. JavaScript has several primitive data types.
String
The String object is used to represent and manipulate a sequence of characters. Strings are useful for holding data that can be represented in text form.
Example: names, messages, or sentences.
const string1 = "A string primitive";
const string2 = 'Also a string primitive';
const string3 = `Yet another string primitive`;
Number
Numbers represent numeric values. Numbers are most commonly expressed in literal forms like 255 or 3.14159.
let age = 25;
let price = 199.99;
// JavaScript uses one number type for both integers and decimals.
Boolean
A boolean represents either true or false. Booleans are often used in conditions and decision making.
let isStudent = true;
let isLoggedIn = false;
Null
null means the variable currently has no value assigned intentionally.
let selectedUser = null;
Undefined
undefined means the variable has been declared but not assigned a value.
let score;
console.log(score); // undefined
Difference between var, let, and const
var is globally scoped, can be redeclared and updated, and is hoisted with a default value of undefined. This can lead to unexpected behavior and bugs.
let is block-scoped (within {}), cannot be redeclared in the same scope, but can be updated. It is hoisted but remains in a "temporal dead zone" until declared, throwing a ReferenceError if accessed before initialization.
const is also block-scoped, cannot be redeclared or reassigned after initialization, and must be assigned a value at declaration. While the variable binding is immutable, properties of objects or elements of arrays declared with const can still be modified.
Note - Use const for values that should not change, let for values that will change, and avoid var in modern JavaScript due to its broader scope and hoisting quirks.
What is Scope? (Beginner Explanation)
Scope defines where a variable can be accessed in the code. Think of scope like rooms in a house.
If a variable is created inside a room, it can only be used inside that room.



