Review Examples¶
As always, we'll kick off with a variety of examples to ignite your pattern-recognition powers. Consider what might differentiate the usage of let
from const
in the examples below.:
Variables Explainer¶
You might notice that some variables are declared with let
and some with const
. The variable let
allows you to declare variables that can be reassigned later. It's like a bookshelf where you can swap out books as you please.
In the above example, you started with "Pride and Prejudice" as your current read but later changed to "Moby Dick". This is what the let
variable was made for. It is totally fine!
On the other hand, const
stands for "constant". Once you've set a value of a const
variable, you can't change it. It's like placing a rare book in a hermetically locked display, and then also throwing away the key – once the book is in, it's there to stay!
Trying to change the classic book to "1984" will result in an error because const variables are immutable (the variable itself cannot be re-assigned to a new value).
One More Example...¶
Now, imagine you're overseeing the operations of the "Literary Treasures Collection," a library that's been around since 1922, located at '123 Literature Lane, Booksville'. You're keeping track of books that are currently on loan, like 'The Great Gatsby', and which book might be borrowed next, say, 'Brave New World'. You're also managing the library's opening hours, which are currently from '10am to 6pm', and noting that each person can borrow up to 5 books. However, some details, like the library's digital catalog or the date of the next inventory check, haven't been set yet, so they're either undefined or deliberately set to null. Here is how that data would be organized by assigning it to variables:
Practice Problems¶
- Declare a variable to store your first name as a string.
- Declare a variable to store your age, which may change in the future.
- Declare a variable to store a boolean value indicating whether you are a student or not.
- Declare a variable to store your favorite colors.
- Declare a variable to store a book title.
- Declare a variable to store the current year.
- Declare a variable to store a boolean value indicating whether it's raining today or not (this value may change).
- Declare a variable to store the value
null
. - Declare a variable without assigning any initial value.
- Declare a variable to store the result of multiplying two numbers together (e.g., 5 * 7).
Answers¶
const firstName = "John";
Since your first name is unlikely to change, we can useconst
.let age = 25;
Your age will change over time, so we uselet
.let isStudent = true;
Once you graduate, you will no longer be a student, so we should uselet
.let favoriteColors = "red and purple";
Your favorite colors could change, so we can uselet
.const book = "The Great Gatsby";
The book title is not expected to change, so we can useconst
.let currentYear = 2024;
The current year changes over time, so we can uselet
.let isRaining = false;
The value indicating whether it's raining or not may change, so we use let.const nullValue = null;
Null is a fixed value, so we can useconst
.let undefinedValue;
When declaring a variable without an initial value, we always use let.const product = 5 * 7;
The result of multiplying two numbers is a fixed value, so we can useconst
.
Practice with AI¶
Use the prompt below to get even more practice problems from any Large Language Model / AI of your choice:
Act as my JavaScript tutor. I am a beginner learning about variables 'let' and 'const' in JavaScript for the first time. Generate 10 practice problems that require declaring and setting different value types into JavaScript variables, based on the extracurricular interest or hobby of [insert your interest or hobby here]. Only provide the problem statements, not the answers.
🎉🎉🎉 Congrats, you've completed your second lesson! When you're ready, you can move on to the next lesson. 🎉🎉🎉