JavaScript Variables โ€“ Introduction

๐Ÿ“˜ What is a Variable?

A variable is a container used to store data values. In JavaScript, variables allow you to store, update, and use data throughout your program.

๐ŸŽฏ Why Do We Use Variables?

  • Store data (numbers, text, objects)
  • Reuse values easily
  • Make code readable and maintainable

๐Ÿงฑ Declaring Variables in JavaScript

JavaScript provides three ways to declare variables:

  • var โ€“ Old way (function scoped)
  • let โ€“ Modern way (block scoped)
  • const โ€“ Constant value (cannot be reassigned)

๐Ÿ’ป Variable Examples

var name = "Ahmed";
let age = 25;
const country = "Egypt";

age = 26; // allowed
// country = "USA"; โŒ not allowed
            
โš ๏ธ Note: Always prefer let and const instead of var.

๐Ÿ“ Variable Naming Rules

  • Must start with a letter, $ or _
  • Cannot start with a number
  • No spaces allowed
  • Case-sensitive (age โ‰  Age)

๐Ÿงช Try It Yourself

Click the button to create variables and display them: