JavaScript Variables
JavaScriptVariables store data. JavaScript has 3 ways to declare variables:
let— can be changed later (use this most!)const— cannot be changed (use for constants)var— old way, avoid in modern code
Data Types
JavaScript automatically detects the type: string, number, boolean, array, object.
// String
let name = "Priya";
// Number
let age = 20;
// Boolean
const isStudent = true;
// Array
let skills = ["HTML", "CSS", "JS"];
// Object
let person = {
name: "Rahul",
age: 22,
city: "Mumbai"
};
console.log(name); // Priya
console.log(skills[0]); // HTML
console.log(person.city); // Mumbai
▶ "HTML"
▶ "Mumbai"
Interview Questions — JavaScript Variables
5 questions commonly asked in interviews
Variables are used to store data values like numbers, strings, and objects.
Using var, let, or const keywords.
var is function scoped, let is block scoped, const is constant and cannot be reassigned.
Scope defines where a variable can be accessed.
The reference cannot change, but object contents can.
Frequently Asked Questions
Common doubts about JavaScript Variables
Prefer const, then let. Avoid var.
var allows redeclaration, let and const do not.
No.
Yes.
Yes.
Test Your Knowledge
5 questions · Earn 50 XP
More on JavaScript Variables
Cheatsheet, tips, resources & what to learn next
Quick Cheatsheet
var x = 10;
let y = 20;
const z = 30;
let name = \"John\";
let age = 25;
Pro Tips
Use const by default.
Use let when value changes.
Avoid var in modern JS.
Use meaningful names.
Follow naming conventions.