Chapter 06

JavaScript Variables

A variable is a named storage location used to store and work with data in a program.

What is JavaScript Variables?

  • Variables can store text, numbers, Boolean values, objects, and other data.
  • A variable has a name that allows the stored value to be accessed later.
  • Modern JavaScript commonly uses let and const for variable declarations.
  • Meaningful variable names make programs easier to understand.

Syntax

let variableName = value;

let studentName = 'Rahul';
let marks = 85;

Example

Try understanding this code before running it.
let studentName = 'Rahul';
let age = 20;
let course = 'Web Development';

console.log(studentName);
console.log(age);
console.log(course);

Important Points

  • Variable names can begin with a letter, underscore, or dollar sign.
  • Variable names cannot contain spaces.
  • JavaScript variable names are case-sensitive.
  • Use meaningful names such as studentName and totalMarks.

Common Mistakes

  • Using spaces in variable names.
  • Using a reserved keyword as a variable name.
  • Using confusing or meaningless variable names.

Real-World Use

Variables are used to store student marks, product prices, user names, login status, shopping cart data, and much more.

Practice Exercise

Practice: Variables
Create variables named studentName, studentAge, and courseName. Display all three values in the console.

Ready to write code?

Open the JavaScript editor and run your code with live output.

Try It Yourself →