๐
๐จ
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 neededvar- Old style, avoid using1// const - value cannot change2const name = "Alice";3// name = "Bob"; // ERROR!45// let - value can change6let age = 25;7age = 26; // OK!89// var - old way, don't use10var score = 100; // avoid this
๐ค
๐จ
Data Types
Variables can hold different types of data:
1// String - text in quotes2const greeting = "Hello, World!";34// Number - integers and decimals5const count = 42;6const price = 19.99;78// Boolean - true or false9const isActive = true;10const isGameOver = false;1112// Undefined - no value yet13let username;14console.log(username); // undefined1516// Null - intentionally empty17const 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!";34// You can change message:5message = "New value"; // Works!67// 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