New: Complete Beginner's Guide to Coding is now available in Premium
Updated: Indian Govt Exam roadmaps now include salary breakdowns & timelines
Tip: Use the Career Hub to explore all career paths in one place
Tutorials JavaScript JavaScript Variables

JavaScript Variables

JavaScript

Variables 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.

Example — JAVASCRIPT
// 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
Result
▶ "Priya"
▶ "HTML"
▶ "Mumbai"

Interview Questions — JavaScript Variables

5 questions commonly asked in interviews

Q1 What are variables in JavaScript? Beginner

Variables are used to store data values like numbers, strings, and objects.

Q2 How do you declare variables? Beginner

Using var, let, or const keywords.

Q3 Difference between var, let, const? Intermediate

var is function scoped, let is block scoped, const is constant and cannot be reassigned.

Q4 What is variable scope? Intermediate

Scope defines where a variable can be accessed.

Q5 Can const variables change? Advanced

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

0 / 5
Q1 Which keyword is block scoped?
Q2 Which cannot be reassigned?
Q3 Valid variable?
Q4 JS is case?
Q5 Which stores value?

More on JavaScript Variables

Cheatsheet, tips, resources & what to learn next

Quick Cheatsheet

var
var x = 10;
let
let y = 20;
const
const z = 30;
String
let name = \"John\";
Number
let age = 25;

Pro Tips

1

Use const by default.

2

Use let when value changes.

3

Avoid var in modern JS.

4

Use meaningful names.

5

Follow naming conventions.

Up Next

JS Operators JS Conditions JS Loops JS Functions JS Arrays