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 prowess. While you peruse these samples, consider what might differentiate the usage of let from const. Also, do you see any unusual or unfamiliar looking data types below?

You might notice that some variables are declared with let and some with const. In the realm of JavaScript, the way you store data is crucial. 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 totally fine!

On the other hand, const stands for "constant". Once you've set a value to a const variable, you can't change it. It's like placing a rare book in a hermeneutically 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).

Null & Undefined

You also may have noticed a couple new data types that were snuck into the examples above - undefined and null. In JavaScript, when we talk about undefined and null, we're referring to two special ways to describe a lack of value. Think of undefined as an empty shelf in a bookstore. The shelf exists, but there's no book on it yet; it's waiting to hold something in the future. On the other hand, null is like a deliberate decision to leave a shelf empty. It's us saying, "We know there's no book here, and that's intentional." Maybe we put a cardboard box the size of a book in that space , in order to hold on to the space where we don't want a book placed. While both undefined and null represent the absence of a value, undefined suggests we haven't given it a value yet, while null means we've chosen to have no value there. You will see later how null comes in handy.

So we can now add these two primitive data types to our list. We now know about strings, numbers, booleans, undefined, and null.

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.

// 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";

Enhance Your Learning With ChatGPT