Facebook Pixel

Variables in JavaScript

Let’s talk about one of the most fundamental topics in JavaScript — variables. If you’ve ever wondered how programs “remember” things or keep track of data, the answer is — variables.

What Are Variables?

In JavaScript, variables are used to store data.

You can think of a variable like a container or a box that holds information. You can put something inside it, take it out, or even replace it with something new later.

For example:

var name = "Harry";
var age = 25;

Here, name and age are variables that store the values "Harry" and 25.

Variables allow you to store, retrieve, and manipulate data in your program — which is what makes them one of the most important concepts in any programming language.

Declaring Variables

In JavaScript, there are three main ways to declare variables:

  1. Using var
  2. Using let
  3. Using const

Let’s go through each of them one by one.

1. Using var

The var keyword is the oldest way to declare a variable in JavaScript.

Example:

var x = 10;

This creates a variable named x and assigns the value 10 to it. If you want, you can also declare it first and assign a value later:

var x;
x = 10;

However, var has some scoping issues — it doesn’t follow block-level scope (we’ll discuss this later). That’s why modern JavaScript mostly uses let and const.

2. Using let

The let keyword was introduced in ES6 (ECMAScript 2015). It’s the modern and recommended way to declare variables that can change their values later.

Example:

let count = 5;
count = 10; // works fine

You can reassign a let variable, but you cannot redeclare it in the same scope.

let name = "Harry";
let name = "CodeWithHarry"; // This will throw an error

3. Using const

The const keyword is used to declare constants — variables whose values cannot be changed once assigned.

Example:

const pi = 3.14;
pi = 3.14159; // Error: Assignment to constant variable

You must assign a value to a const variable at the time of declaration — leaving it empty will also cause an error:

const num; // Error

However, if the constant holds an object or array, the contents of that object can still change (only the reference is constant):

const arr = [1, 2, 3];
arr.push(4); // Works fine
console.log(arr); // [1, 2, 3, 4]

Quick Recap: var vs let vs const

Keyword Scope Re-declaration Re-assignment Default Usage
var Function scope Allowed Allowed Legacy (avoid in modern code)
let Block scope Not allowed Allowed Use when the value will change
const Block scope Not allowed Not allowed Use for constants or fixed values

JavaScript Is Dynamically Typed

JavaScript is a dynamically-typed language. That means you don’t need to declare the data type of a variable — it’s automatically decided based on the value you assign.

Example:

let x = 10;       // number
x = "Hello";      // now it's a string
x = [1, 2, 3];    // now it's an array

The type changes as the value changes — simple as that.

Variable Naming Rules

When naming variables in JavaScript, you need to follow a few rules:

Variable names can include:

  • Letters (a-z, A-Z)
  • Digits (0-9)
  • Underscores (_)
  • Dollar signs ($)

But:

  • Variable names cannot start with a digit.
  • They are case-sensitive (Name and name are different).

Good practice:

  • Use meaningful and descriptive names (e.g., userName, totalPrice, isLoggedIn).
  • Use camelCase for multi-word variables.

Examples:

let userName = "Harry";
let user_age = 25;
let $balance = 1000;

Bad example:

let 2name = "John"; // Invalid
let var = 10; // 'var' is a reserved word

Using Variables

Once declared, you can use variables to store, update, and work with data in your programs.

Example:

let x = 10;
console.log(x); // prints 10
 
x = "hello";
console.log(x); // prints "hello"

You can also perform operations:

let a = 10;
let b = 20;
let sum = a + b; // 30
 
let str1 = "Code";
let str2 = "WithHarry";
let fullStr = str1 + str2; // "CodeWithHarry"