Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Different Error shown in Hoisting of example

To better understand the difference in hoisting, I tried this function and came out with a different error from the video.

console.log( getCandy() );
const getCandy = function(){
    const favCandy ='skittles';
    return(`I love ${favCandy}!`);
};

Uncaught ReferenceError: getCandy is not defined at <annoymous>1:14

For reference the time stamp is 2:35-3:01, when it is shown the error that occurs when you try to hoist a Function Expression Vs Declaration.

Am I getting essentially the same type error? or is this something totally different from what the video is trying to convey?

1 Answer

Steven Parker
Steven Parker
231,269 Points

You forgot to provide a link the video, but I tried this code myself and got this error: "*Uncaught ReferenceError: Cannot access 'getCandy' before initialization *"

This slightly different wording is essentially the same thing you got, telling us that since the definition is not hoisted, it is undefined when the attempt is made to call it in the console.log statement prior to the assignment.

If you change the assignment to named function declaration, it does get hoisted and no error occurs.