Think of variables as labeled containers that hold information in JavaScript. Just like you might put cookies in a jar labeled "cookies," variables store data with descriptive names.
Variables are essential because they remember information like user input, scores, or settings.
Engagement Message
What kind of information might a shopping website need to remember about you?
In JavaScript, you create variables using special keywords. The most common one is let
. Here's the basic pattern:
let userName = "Sarah";
This creates a container called userName
and puts the text "Sarah" inside it.
Engagement Message
What do you think the equals sign =
does in this example?
JavaScript can store different types of data. Numbers are one important type—they can be whole numbers like 42
or decimals like 3.14
.
let age = 25;
let price = 19.99;
Both of these store numeric values that you can use for calculations.
Engagement Message
Can you think of two numbers a website might need to store?
Text data is called a in JavaScript. Strings are always wrapped in quotes—either single quotes or double quotes .
