๐Ÿ“ฆ

Variables

๐ŸŽ‰
๐Ÿ’จ
Variables are like labeled boxes where you store your stuff!

What are Variables?

A variable is a container that stores data. Think of it like a labeled box - you give it a name and put something inside.

Later, you can use the name to get the value back, or even change what's inside!

๐Ÿ’ก
๐Ÿ’จ

Three Ways to Create Variables

JavaScript gives you three keywords to create variables:

const- Cannot be reassigned (use by default!)
let- Can be reassigned when needed
var- Old style, avoid using
1// const - value cannot change
2const name = "Alice";
3// name = "Bob"; // ERROR!
4
5// let - value can change
6let age = 25;
7age = 26; // OK!
8
9// var - old way, don't use
10var score = 100; // avoid this
๐Ÿค”
๐Ÿ’จ

Data Types

Variables can hold different types of data:

1// String - text in quotes
2const greeting = "Hello, World!";
3
4// Number - integers and decimals
5const count = 42;
6const price = 19.99;
7
8// Boolean - true or false
9const isActive = true;
10const isGameOver = false;
11
12// Undefined - no value yet
13let username;
14console.log(username); // undefined
15
16// Null - intentionally empty
17const selectedItem = null;
๐ŸŽฎ

Try It Yourself!

Experiment with let and const below. Try reassigning values!

let messagecan change

Current value: "Hello"

const lockedcannot change
1let message = "Hello";
2const locked = "Cannot change me!";
3
4// You can change message:
5message = "New value"; // Works!
6
7// But not locked:
8// locked = "Try me"; // Error!

๐Ÿงช Code Lab - Try It Yourself!

๐ŸงชVariables Lab
Enter RunShift+Enter New line History
๐Ÿ“‹Output

Run some code to see the output here!

๐Ÿ’กTry These Examples