03JavaScript
Variables – Storing Data
Easy15MIN
What Is a Variable?
A variable is a name that you use refer to something, such as a saved number or text. Imagine a variable as a wire that connects a name to a value.
Creating a Variable with let
One way to create a variable in JavaScript is by using the let keyword. With let, you create a "wire" that can later be reconnected to a new value. This is useful because, as you learn, you’ll see that the value your variable points to might change as your program runs.
Here’s a simple example:
JAVASCRIPT
1let score = 10;
2console.log(score);In this code:
- We create a wire (variable) called score using let.
- We connect (or point) that wire to the value 10.
- We then print the value to the console with console.log(score).
Changing What a Variable Points To
Because the wire created with let can be reconnected, you can change its value at any time. For instance:
JAVASCRIPT
1let greeting = "Hello";
2greeting = "Hi there";
3console.log(greeting); // Prints: Hi thereThe wire named greeting now points to “Hi there.”
Exercises
1 / 2
Hakatemia Pro
Learn to hack — start here
Hundreds of interactive courses, virtual labs and CTF challenges in your browser. Start a free trial — no card required.