EASYTUTORGUIDE

Practical tutorials, tools, courses, digital skills, and business promotion.

Free Learning

Chapter 4: JavaScript Variables

Learn all about javascript from very beginners to experts.

Beginner Friendly Examples Output Explanation
Javascript Lesson 1 Introductions interactive Courses

Main reading content

Chapter 4: Variables

Variables are containers that store data in JavaScript. In this chapter, you will learn var, let, const, scope, hoisting, naming rules, reassignment, memory allocation, and best practices.

4.1 var

var is an older way to create variables in JavaScript. It can be changed after creation.

Example

// Create a variable using var
var name = "Majid";

// Print the value
console.log(name);

// Change the value
name = "EasyTutorGuide";

// Print the new value
console.log(name);

// Output:
// Majid
// EasyTutorGuide

Explanation: The variable name first stores "Majid". Later, we change it to "EasyTutorGuide".

4.2 let

let is the modern way to create variables that can change.

Example

// Create a variable using let
let age = 20;

// Print age
console.log(age);

// Change age
age = 21;

// Print new age
console.log(age);

// Output:
// 20
// 21

Explanation: Use let when the value may change later.

4.3 const

const is used for values that should not be reassigned.

Example

// Create a constant variable
const country = "Canada";

// Print the value
console.log(country);

// This would cause an error:
// country = "USA";

// Output:
// Canada

Explanation: A const variable cannot be assigned a new value.

4.4 Variable Scope

Scope means where a variable can be used in your code.

Example

// Global variable
let city = "Toronto";

function showCity() {
  // This function can use the global variable
  console.log(city);
}

showCity();

// Output:
// Toronto

Explanation: The variable city is outside the function, so it can be used inside the function.

4.5 Variable Hoisting

Hoisting means JavaScript moves some declarations to the top before running code.

Example with var

// Using var before declaration
console.log(score);

var score = 90;

// Output:
// undefined

Explanation: With var, JavaScript knows the variable exists, but the value is not assigned yet.

4.6 Naming Rules

Variable names must follow rules. They can contain letters, numbers, underscores, and dollar signs, but they cannot start with a number.

Example

// Good variable names
let firstName = "Ali";
let user_age = 15;
let $price = 25;

// Bad variable name:
// let 1name = "Ali";

// Print values
console.log(firstName);
console.log(user_age);
console.log($price);

// Output:
// Ali
// 15
// 25

Explanation: Use clear names like firstName, totalPrice, and userAge.

4.7 Constants

Constants are values that should stay the same.

Example

// Tax rate should not change in this program
const TAX_RATE = 0.13;

let price = 100;
let tax = price * TAX_RATE;

console.log(tax);

// Output:
// 13

Explanation: We use uppercase names like TAX_RATE for important constant values.

4.8 Reassignment

Reassignment means giving a variable a new value.

Example

// let allows reassignment
let points = 10;

points = 20;

console.log(points);

// const does not allow reassignment
const level = 1;

// This would cause an error:
// level = 2;

// Output:
// 20

Explanation: Use let if the value changes. Use const if the value should stay the same.

4.9 Memory Allocation

When you create a variable, JavaScript stores its value in memory.

Example

// JavaScript stores this number in memory
let numberOne = 50;

// JavaScript stores this string in memory
let message = "Hello JavaScript";

// Print both values
console.log(numberOne);
console.log(message);

// Output:
// 50
// Hello JavaScript

Explanation: Every variable needs memory so JavaScript can remember its value.

4.10 Best Practices

Good variable habits make your code easier to read and fix.

Example

// Good practice: use clear names
const taxRate = 0.13;
let productPrice = 200;
let totalTax = productPrice * taxRate;

console.log(totalTax);

// Output:
// 26

Explanation: Clear names help beginners understand what each variable does.

Chapter 4 Summary

Use const by default. Use let when the value changes. Avoid var in modern JavaScript.

Chapter 4: VariableBuild Your Coding Experience

A modern course built to help learners study step by step with clarity, comfort, and confidence.