Variables

Loading concept...

JavaScript Variables: Your Magic Storage Boxes

The Story of the Forgetful Chef

Imagine you’re a chef in a busy kitchen. Every day, you cook amazing dishes. But there’s a problem: you keep forgetting your recipes! One day you make the perfect soup, but the next day… poof! Gone from your memory.

What if you had magic storage boxes where you could write down and save anything? That’s exactly what variables are in JavaScript!

A variable is like a labeled box where you store information. You give it a name, put something inside, and whenever you need it, you just call its name!


Meet the Three Box Types

In JavaScript, we have three special ways to create storage boxes:

Box Type Can Change? Where Works? Best For
let Yes Only inside its room Most things!
const No Only inside its room Things that never change
var Yes Everywhere in the house Old-style (avoid it!)

Variables with let

What is let?

Think of let like a whiteboard in your room. You can write something on it, erase it, and write something new!

let myAge = 10;
// Later, on your birthday...
myAge = 11;

When to Use let

Use let when your stored value might change:

  • A game score that goes up and down
  • The weather that changes each day
  • Your mood (happy, sleepy, excited!)

Example: Counting Candies

let candies = 5;
// You eat 2 candies
candies = candies - 2;
// Now candies = 3

The Room Rule (Block Scope)

let only works inside its “room” (the curly braces {}):

if (true) {
  let secret = "pizza";
  // secret exists here!
}
// secret doesn't exist here!

Variables with const

What is const?

const is like a permanent marker on stone. Once you write it, you can’t erase it!

const myBirthday = "January 15";
// This is forever! Can't change it.

When to Use const

Use const for things that should never change:

  • Your name
  • The number of days in a week (always 7!)
  • Math values like pi (3.14159…)

Example: Important Rules

const daysInWeek = 7;
const myName = "Luna";
const pi = 3.14159;

What Happens If You Try to Change It?

const color = "blue";
color = "red"; // ERROR! Can't do this!

JavaScript will stop and say: “Hey! You promised this would never change!”


Variables with var

What is var?

var is the old grandpa of variables. It was here before let and const existed!

var oldSchoolVariable = "I'm old!";

Why We Don’t Use var Much Anymore

var is like a loud announcer - it shouts everywhere in the house:

if (true) {
  var leaked = "I escaped!";
}
// leaked still exists here! (Oops!)

This can cause bugs! That’s why we prefer let and const.

The One Time var Still Appears

You’ll see var in older code. It still works, but modern JavaScript coders use let and const instead.


Variable Naming Rules

The Five Golden Rules

Just like you can’t name your pet “!!!123”, variables have rules too!

graph LR A[Naming Rules] --> B[Start with letter, $, or _] A --> C[Can include numbers after first character] A --> D[No spaces allowed] A --> E[Case sensitive] A --> F[No reserved words]

Rule 1: Start Right

Variables must start with:

  • A letter (a-z, A-Z)
  • A dollar sign ($)
  • An underscore (_)
let userName = "good";
let $price = 100;
let _secret = "shh";
let 123abc = "bad"; // ERROR!

Rule 2: Numbers Come Later

Numbers can appear, but not at the start:

let player1 = "Mario";    // OK
let score99 = 1000;       // OK
let 1stPlace = "Winner";  // ERROR!

Rule 3: No Spaces

Use camelCase instead of spaces:

let myFavoriteColor = "blue";  // OK
let my favorite color = "red"; // ERROR!

Rule 4: Case Matters

These are all different variables:

let apple = "red";
let Apple = "green";
let APPLE = "yellow";
// Three different boxes!

Rule 5: Be Descriptive

Good names tell a story:

// Hard to understand
let x = 25;

// Easy to understand
let playerAge = 25;

Reserved Words

What Are Reserved Words?

Reserved words are like VIP names that JavaScript already uses. You can’t use them for your variables!

Think of it like this: You can’t name your dog “Mom” or “School” because those words already mean something important!

The Most Common Reserved Words

// These are TAKEN by JavaScript:
// let, const, var
// if, else, while, for
// function, return
// true, false, null
// class, new, this

Example of What NOT to Do

let if = 5;       // ERROR!
let function = 3; // ERROR!
let class = "A";  // ERROR!

What You CAN Do

let myIf = 5;           // OK!
let myFunction = 3;     // OK!
let classGrade = "A";   // OK!

Quick Decision Guide

graph TD A[Need to store something?] --> B{Will it change?} B -->|Yes| C[Use let] B -->|No| D[Use const] E[Avoid var!] --> F[It's outdated]

Summary: What We Learned

Concept Key Point
let For values that can change
const For values that never change
var Old style, avoid in new code
Naming Start with letter, $, or _
Reserved Some words are taken by JavaScript

Your Confidence Boost

You now know the secret of JavaScript storage! Every app, game, and website uses variables. When you see:

let score = 0;
const maxLives = 3;

You’ll smile and say: “I know exactly what those magic boxes are doing!”

Variables are the foundation of everything in programming. Master these, and you’re on your way to becoming a coding wizard!

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.