Lesson 8 of 30 – JavaScript Variables
27%

JavaScript Variables

A variable is a container used to store data in JavaScript. The stored value can be changed and reused throughout the program.

JavaScript provides three ways to declare variables: var, let, and const.

Note: Modern JavaScript recommends using let for variables and const for fixed values.
What is a Variable?

A variable stores information that can be used later in a JavaScript program.

Examples:
let name = "Steve";
let age = 20;
let isStudent = true;

Variables can store text, numbers, Boolean values, objects, arrays, and more.

Ways to Declare Variables

JavaScript provides three keywords for declaring variables.

Keyword Description
var Old method, function-scoped.
let Modern and recommended for changing values.
const Used for values that should not be reassigned.

For most programs, use let and const.

Variable Declaration

A variable can be declared without assigning a value.

Example:
let myVariable;

The default value of an uninitialized variable is undefined.

Variable Initialization

You can assign a value while declaring a variable or later.

Example:
let message;
message = "Welcome to JavaScript!";

The assignment operator (=) stores the value in the variable.

Declaration and Initialization Together

Most programmers declare and initialize variables in one statement.

Example:
let name = "Lovely";
let num = 100;
let isActive = true;

Variables can store different types of values.

Multiple Variables

You can declare multiple variables in a single line.

Example:
let name = "Rita",
num = 100,
isActive = true;

This saves space and keeps related variables together.

Copying Variables

The value of one variable can be copied into another variable.

Example:
let num1 = 100;
let num2 = num1;

Now both variables contain the value 100.

Whitespace and Line Breaks

JavaScript allows extra spaces and line breaks when declaring variables.

Example:
let name = "Steve",
num = 100,
isActive = true;

Proper formatting makes code easier to read.

Variable Naming Rules

JavaScript follows certain rules for naming variables.

Rule Example
Variable names are case-sensitive. name and Name are different.
Can contain letters, digits, _ and $. user1, _name, $price
Cannot start with a digit. ❌ 1name
Cannot use reserved keywords. ❌ var, function, return
let userName;
let student_id;
let $price;
Case Sensitivity

JavaScript treats uppercase and lowercase letters differently.

Variable 1 Variable 2 Same?
name Name No
msg MSG No
age Age No

Always use consistent naming throughout your program.

Dynamic Typing

JavaScript is a dynamically typed language.

A variable can store different types of values during program execution.

Example:
let data = 1;

data = "One";

data = 1.5;

data = true;

data = null;

The same variable can hold numbers, strings, booleans, and other data types.

Constant Variables

Use the const keyword for values that should not be reassigned.

  • Must be initialized during declaration.
  • Cannot be assigned a new value later.
  • Helps prevent accidental changes.
Example:
const PI = 3.14159;

const country = "India";
Rules for const Variables

Constant variables have special rules.

Invalid Examples:
const num = 100;
num = 200; // Error

const name; // Error

A const variable must have a value when it is declared.

Constant Objects

A const object cannot be reassigned, but its properties can be modified.

Example:
const person = {
name: "Kunal"
};

person.name = "Raj";

alert(person.name);

The object itself remains the same, but its contents can change.

Best Practices for Variables
  • Use let for changing values.
  • Use const whenever possible.
  • Avoid using var in modern JavaScript.
  • Choose meaningful variable names.
  • Use camelCase naming style.
  • Avoid single-letter variable names unless necessary.
  • Do not use reserved keywords as variable names.
Variable Scope

In JavaScript, variables can have either global scope or local scope.

  • Global variables can be accessed anywhere in the program.
  • Local variables can only be accessed inside the function where they are declared.
Global Variables

Variables declared outside any function are called global variables.

They can be used anywhere in the JavaScript program.

let message = "Welcome";

function show(){
alert(message);
}
Local Variables

Variables declared inside a function are called local variables.

They can only be accessed within that function.

function show(){
let msg = "JavaScript";
alert(msg);
}

show();

Trying to access msg outside the function will cause an error.

Global and Local Variable Example

A function can access both global and local variables.

let greet = "Hello ";

function myFunction(){
let msg = "JavaScript!";
alert(greet + msg);
}

myFunction();

alert(greet);

The global variable can be accessed everywhere, but the local variable exists only inside the function.

Variables Without let or var

JavaScript allows variables to be created without using let or var.

Such variables become global variables and may create unexpected problems.

Example:
function myFunction(){
msg = "Hello JavaScript!";
}

myFunction();

alert(msg);
Best Practice: Always declare variables using let or const.
Variables Summary
  • Variables store data values.
  • Use let for changeable values.
  • Use const for fixed values.
  • Avoid using var in modern JavaScript.
  • Variable names are case-sensitive.
  • JavaScript supports dynamic typing.
  • Variables can have global or local scope.
  • Always declare variables properly.
Best Practices
  • Use meaningful variable names.
  • Prefer let and const.
  • Avoid creating global variables unnecessarily.
  • Use camelCase naming convention.
  • Keep variable names simple and descriptive.
  • Initialize variables before using them.
  • Use constants for values that should not change.

🧠 Quick Quiz

Which keyword is recommended for declaring variables that can change?