

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Functions
incomplete
2: Function Hoisting
incomplete
3: Unit Test Lessons
incomplete
4: Multiple Return Values
incomplete
5: Functions As Values
incomplete
6: Scope
incomplete
7: Anonymous Functions
incomplete
8: Default Parameters
incomplete
9: Passing by Value
incomplete
10: Immediate Invocation
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
In Python, a function must be defined before any code that executes it. But that's not so in JavaScript! As long as a function is defined somewhere in the file, it can be called even before the definition.
console.log(getLabel(3));
// prints 'awful'
function getLabel(numStars) {
if (numStars > 7) {
return "great";
} else if (numStars > 3) {
return "okay";
} else {
return "awful";
}
}
This works because JavaScript "hoists" the function declaration to the top of the file before the code is executed.