🧮 JavaScript Variables & Data Types
Estimated reading: 4 minutes 197 views

JavaScript Variables – The Foundation of Storing Data in JS

Ever wondered how JavaScript remembers values like your name, age, or login status?

That’s exactly where variables come in. Variables in JavaScript act like containers — storing data that your scripts can use and manipulate at any point in the program.

Whether you’re building a form validation, an interactive app, or a complex web game, understanding variables is essential.


What Is a JavaScript Variable?

A variable is a named container that holds a value. Think of it as a label for a value in memory, which you can reuse or update.

JavaScript variables can store:

  • Numbers
  • Strings
  • Booleans
  • Objects
  • Arrays
  • Functions

Primary Keywords: JavaScript variables, var, let, const, declare variable in JS


Declaring Variables in JavaScript

JavaScript uses three main keywords to declare variables:

KeywordScopeReassignableHoistingBlock Scoped
varFunction Yes Yes No
letBlock Yes No Yes
constBlock No No Yes

Syntax of JavaScript Variables

Using var

codevar name = "Alice";
console.log(name);

Explanation:

  • var declares a variable.
  • name is the variable name.
  • "Alice" is the string value assigned.
  • console.log(name) prints the value to the console.

var is function-scoped, and may cause unintended behavior due to hoisting.


Using let

codelet age = 30;
console.log(age);

Explanation:

  • let is block-scoped, so it’s safer in modern code.
  • age stores the number 30.

Using const

codeconst country = "India";
console.log(country);

Explanation:

  • const is for constants—values that should not be changed.
  • Once declared, you cannot reassign a const variable.

Reassigning and Updating Variables

codelet score = 10;
score = 15;
console.log(score); // Output: 15

With let, reassignment is allowed.


Trying to Reassign a const

codeconst pi = 3.14;
pi = 3.1415; // Error: Assignment to constant variable

Error Explanation: const values cannot be reassigned.


Variable Naming Rules in JavaScript

Valid variable names:

  • Must start with a letter, _, or $
  • Can include numbers after the first character
  • Are case-sensitive

Invalid names:

codelet 123name = "error"; //  Cannot start with a number
let let = "error"; // Cannot use reserved keywords

Best Practices for Declaring Variables

✔️ Use const by default — it’s safer and prevents accidental changes
✔️ Use let only when the value needs to change
Avoid var in modern code (due to scoping issues)

Pro Tip: Always declare your variables at the top of the block to avoid hoisting-related bugs.


Practical Use Case: Swapping Values

codelet a = 5;
let b = 10;

let temp = a;
a = b;
b = temp;

console.log("a:", a); // 10
console.log("b:", b); // 5

You’re storing one value temporarily while swapping — classic use of variables.


Storing Multiple Data Types

codelet userName = "John";         // String
let age = 25; // Number
let isAdmin = true; // Boolean
let hobbies = ["reading", "gaming"]; // Array
let address = { city: "Delhi", pincode: 110011 }; // Object

JavaScript variables are dynamically typed, meaning the type is determined at runtime.


Variable Scope in JavaScript

Scope defines where a variable is accessible.

Global Scope

codelet globalVar = "I'm global";

function show() {
console.log(globalVar); // Accessible
}
show();

Local (Function) Scope

codefunction greet() {
let message = "Hello!";
console.log(message); // Accessible here
}
greet();
// console.log(message); Error – not accessible outside

Block Scope with let / const

code{
let blockScoped = "Inside block";
console.log(blockScoped); //
}
// console.log(blockScoped); Error – out of scope

Hoisting Behavior

var is hoisted to the top of its scope but not its value:

codeconsole.log(x); // undefined
var x = 5;

Internally behaves like:

codevar x;
console.log(x); // undefined
x = 5;

let and const are not initialized during hoisting and cause a ReferenceError if accessed early.


Comparison Table: var vs let vs const

Featurevarletconst
ScopeFunctionBlockBlock
Can be reassigned Yes Yes No
Can be redeclared Yes No No
Hoisting behaviorHoisted (value: undefined)Hoisted (uninitialized)Hoisted (uninitialized)
Default use Avoid When needed Recommended

Summary

In this guide, you learned:

  • What JavaScript variables are and how to declare them
  • The difference between var, let, and const
  • Best practices and common mistakes to avoid
  • Real-world examples with line-by-line explanation

Understanding variables is your first step toward mastering JavaScript. Use const wherever possible, let when needed, and avoid var unless working with legacy code.


FAQ – JavaScript Variables

Q1: What’s the difference between let and var?
let is block-scoped and doesn’t allow redeclaration, while var is function-scoped and does. Prefer let in modern JS.

Q2: Can you update a const variable?
You can’t reassign a const variable. However, if it holds an object or array, you can modify its contents.

Q3: Are variables case-sensitive in JavaScript?
Yes. Name and name are treated as different variables.

Q4: What happens if I use a variable before declaring it?
If declared with var, it’s hoisted with undefined. With let or const, it throws a ReferenceError.

Q5: Should I always use let instead of var?
Yes. let and const are safer and follow block scoping. Use var only in legacy codebases.


Share Now :
Share

JavaScript Variables

Or Copy Link

CONTENTS
Scroll to Top