Code Self Study Forum

Test Yourself: JavaScript Hoisting

Here’s a fun test of JS hoisting knowledge that people might like. Write down your predicted output before running the snippets. :slight_smile:

Version 1

What will this version output and why?

var favoriteFood = "grapes";

var foodThoughts = function() {
    console.log("original favorite: " + favoriteFood);
    var favoriteFood = "sushi";
    console.log("new favorite food " + favoriteFood);
};

foodThoughts();

Version 2

What will this version output and why?

let favoriteFood = "grapes";

const foodThoughts = function() {
    console.log("original favorite: " + favoriteFood);
    favoriteFood = "sushi";
    console.log("new favorite food " + favoriteFood);
};

foodThoughts();

(It’s from these videos.)