«Back to Home

Learn JavaScript

Topics

Variables in JavaScript

Variables are one of the most important concepts in any programming language. A variable is simply a container used to store information. You can save values like numbers, text, or even complex data, and use them whenever needed in your program.

As a college student or fresher, understanding variables properly will help you write better code and avoid common beginner mistakes.

What Is a Variable?

A variable works like a storage box.

You assign a name to the box, and then you put a value inside it.

For example:

let age = 21;

Here:

  • age ? the variable name

  • 21 ? the value stored inside it

You can later use age anywhere in your program.

Why Do We Use Variables?

Variables make your code flexible and reusable.
You use variables to:

  • Store user data

  • Save calculations

  • Hold temporary values

  • Manage text, numbers, or details

  • Build dynamic applications

Without variables, your code would be fixed and limited.

How to Create a Variable

JavaScript gives you three ways to declare variables:

  • var

  • let

  • const

They look similar but behave differently.

Using var (Old Way)

var was used in older JavaScript versions. It still works but has some problems with scope, which we will learn later.

Example:

var city = "Delhi";
console.log(city);

Output:

Delhi

You should avoid using var in modern JavaScript unless required.

Using let (Modern and Recommended)

let is the most commonly used keyword to create variables because it is safe and predictable.

Example:

let name = "Karan";
console.log(name);

Output:

Karan

You can change the value of a let variable later.

Example:

let score = 50;
score = 75;  // value updated

console.log(score);

Output:

75

This makes let perfect for values that change.

Using const (For Values That Never Change)

const is used when you do not want the value to change.

Example:

const pi = 3.14;
console.log(pi);

Output:

3.14

If you try to change it:

pi = 3.99;

You will get an error because const values cannot be updated.

Difference Between var, let, and const

KeywordCan Update Value?Good For
varYesOlder code, not recommended
letYesMost variables in modern code
constNoFixed values

In real-world applications, developers mostly use let and const.

Rules for Naming Variables

When naming variables:

  1. Use letters, numbers, underscore, or dollar sign

  2. Names cannot start with a number

  3. Use simple and meaningful names

  4. Avoid special characters

  5. Use camelCase (recommended)

Examples of valid names:

let firstName;
let student_age;
let totalMarks;

Examples of invalid names:

let 1name;    // cannot start with numberlet user-name; // hyphen not allowed

Storing Different Types of Data

Variables can store different types of values.

Example:

let age = 20;                 // numberlet student = "Riya";         // stringlet isPassed = true;          // booleanlet marks = [50, 60, 70];     // arraylet details = { name: "Riya", age: 20 }; // object

You will learn all these types in the next chapters.

Example Program With Output

let studentName = "Arjun";
let rollNumber = 101;
let isActive = true;

console.log("Student Name:", studentName);
console.log("Roll Number:", rollNumber);
console.log("Active:", isActive);

let marks = 45 + 35 + 40;
console.log("Total Marks:", marks);

const collegeName = "ABC College";
console.log("College:", collegeName);

Output:

Student Name: ArjunRoll Number: 101Active: trueTotal Marks: 120College: ABC College

This example shows how different variables are used in real programs.

Common Mistakes Beginners Make

  1. Using var instead of let

  2. Changing a const value

  3. Using confusing names like x1, temp, etc.

  4. Forgetting to declare variables

  5. Using spaces in variable names

Avoid these mistakes to write clean and error-free code.

Practice Task (Do It Yourself)

Create variables for:

  • Your name

  • Your branch or course

  • Your favorite sport

  • Your age

  • Your current city

Print all values in the console.

Author
Vijayakumar S
0 3.9k 2m