Mona Alfonso

Mona Alfonso

Web Developer and Educator with 5+ years of experience in tech.

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.

let currentRead = "Pride and Prejudice";
currentRead = "Moby Dick";  // Totally fine!

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!

const classic = "To Kill a Mockingbird";
classic = "1984";  // This will throw an error!

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:

// Using let for changeable values
let onLoan = "The Great Gatsby";
let nextUp = "Brave New World";
let openingHours = "10am - 6pm"; 
let maxBooksPerPerson = 5;
let digitalCatalog; // undefined since it hasn't been set yet
let nextInventoryCheck = null; // deliberately set to no value for now
 
// Using const for value that will not change
const libraryName = "Literary Treasures Collection";
const foundingYear = 1922;
const libraryAddress = "123 Literature Lane, Booksville";

Practice Problems

  1. Declare a variable to store your first name as a string.
  2. Declare a variable to store your age, which may change in the future.
  3. Declare a variable to store a boolean value indicating whether you are a student or not.
  4. Declare a variable to store your favorite colors.
  5. Declare a variable to store a book title.
  6. Declare a variable to store the current year.
  7. Declare a variable to store a boolean value indicating whether it's raining today or not (this value may change).
  8. Declare a variable to store the value null.
  9. Declare a variable without assigning any initial value.
  10. Declare a variable to store the result of multiplying two numbers together (e.g., 5 * 7).

Answers

  1. const firstName = "John"; Since your first name is unlikely to change, we can use const.
  2. let age = 25; Your age will change over time, so we use let.
  3. let isStudent = true; Once you graduate, you will no longer be a student, so we should use let.
  4. let favoriteColors = "red and purple"; Your favorite colors could change, so we can use let.
  5. const book = "The Great Gatsby"; The book title is not expected to change, so we can use const.
  6. let currentYear = 2024; The current year changes over time, so we can use let.
  7. let isRaining = false; The value indicating whether it's raining or not may change, so we use let.
  8. const nullValue = null; Null is a fixed value, so we can use const.
  9. let undefinedValue; When declaring a variable without an initial value, we always use let.
  10. const product = 5 * 7; The result of multiplying two numbers is a fixed value, so we can use const.

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. 🎉🎉🎉