JavaScriptLesson 2 of 10Jump to practice
Variables
Store values with let and const, and learn the basic types of value JavaScript works with.
A variable is a name that holds a value, so you can use it later.
let score = 10;
const playerName = "Marcus";
let or const?
| Keyword | Can the value change later? | Use it for |
|---|---|---|
const |
No | Most things. Start with this |
let |
Yes | Counters, totals, anything that updates |
Run this, then try changing city on the last line to see the error const gives you.
<script>
let count = 1;
count = count + 1;
console.log("count is", count);
const city = "Chicago";
console.log("city is", city);
// Remove the two slashes on the next line, then Run:
// city = "Houston";
</script>Result
Types of values
| Type | Examples |
|---|---|
| Number | 42, 3.14, -7 |
| String (text) | "hello", 'Texas' |
| Boolean | true, false |
| Array (a list) | ["red", "green", "blue"] |
| Object (named values) | { name: "Emma", age: 21 } |
<script>
const price = 12;
const quantity = 3;
console.log("Total:", price * quantity);
const firstName = "Emma";
console.log("Hello, " + firstName + "!");
const colors = ["red", "green", "blue"];
console.log("First color:", colors[0]);
console.log("How many:", colors.length);
const student = { name: "Emma", age: 21 };
console.log(student.name, "is", student.age);
</script>Result
Practice
Check what you just learned. Fill in the missing code, then answer the quiz.
Exercise 1Solved
Create a variable
namethat will never change.name = "Sofia";Exercise 2Solved
Create a variable
scorethat will change later.score = 0; score = score + 10;Exercise 3Solved
Print the first item of the array.
const fruits = ["apple", "pineapple", "banana"]; console.log(fruits[]);Arrays start counting at zero.
Finish the practice to complete this lesson.
Sign in to keep your progress on any device.