Understanding Variables and Data Types in JavaScript

Variables — Containers for Storing Data
A variable is a container where you can store data. In JavaScript, there are 3 ways to declare variables:
• var — Old method, function-scoped, can be overwritten anytime
• let — Modern method, block-scoped, value can be changed
• const — Block-scoped, value CANNOT be changed after assignment
Adds element at beginning.
var score = 10; // old way
let name = "Shaaz"; // modern way
const pi = 3.14; // constant, never changes
Tip: Use 'const' by default. Use 'let' only when the value needs to change. Avoid 'var' in modern code.
✅Remember: var is function-scoped. let and const are block-scoped. This difference matters inside loops and if-blocks.
Data Types
JavaScript has 7 main data types. Knowing these is very important for interviews:
1. Number — for all numeric values
let age = 25;
let price = 99.99;
2. String — for text
let name = "Shaaz";
let city = 'Delhi';
3. Boolean — true or false only
let isLoggedIn = true;
let isAdmin = false;
4. Null — intentionally empty
Use null when you want to say 'this variable exists but has no value on purpose'.
let emptyValue = null;
5. Undefined — not assigned yet
When you declare a variable but do not give it a value, it becomes undefined automatically.
let noValue;
console.log(noValue); // Output: undefined
6. Object — key-value pairs
Objects store multiple related values together.
let person = { name: "Shaaz", age: 25, city: "Delhi" };
console.log(person.name); // Output: Shaaz
7. Array — ordered list of values
Arrays store multiple values in a sequence. Each value has an index starting from 0.
Adds element at beginning.
const nums = [2, 3];
nums.unshift(1);
console.log(nums);
// [1, 2, 3]
✅Remember: null means empty on purpose. undefined means not yet assigned. They are NOT the same!
🟢 Tip: Use typeof to check the data type of any variable: console.log(typeof 'hello'); // Output: string


