Learn MORE JavaScript on the ServiceNow Platform: Lesson 4 Exercise - Hoisting
Okay. Quick exercise on hoisting. I want you to demonstrate
the concept of hoisting in JavaScript. Create two functions
one declared using a function declaration and the other with a function expression. Attempt to invoke both before
their declaration and observe the results. And just a reminder for those of you
that are wondering what a function declaration is versus a function
expression, I mean, something like this declaring your function like that
versus doing it via an express function like this. So I want you to do both
and then call both before these the script and let's
see what happens with the hoisting. I'll see you soon. Okay, We're back. Let's go ahead
and look at what you came up with. As I was saying earlier,
I created two functions and one of them was declared
and I called it declared function. And the other one
was be an expression function. So I made it variable express function,
and then I put a function inside of it. Both of them are just doing
a simple log statement of saying some words, and then I try to call both of them
before the actual script. So if I put this inside
ServiceNow, let's see what happens and I'm going to go ahead and run it and you'll see the first one ran, the declared function worked fine, but the expressed function
didn't get hoisted properly. And so now it doesn't know
what to do with it because it doesn't know where
that function is. Because, again, a variable doesn't get hoisted with its set value,
only the variable itself. So that's one of the big differences. And hopefully this was another way
of showing you, even if it's a function inside the variable,
it still doesn't get hoisted properly. Okay. So that's that. Let me know in the comments
if you knew the difference between the two function declarations
and as it pertains to hoisting. See you later.
https://www.youtube.com/watch?v=M-rHuDngtB0