Hoisting
JavaScript reads declarations before running a scope. Function declarations can be called before they appear. Variables declared with let and const exist from the start of the block, but you cannot use them before the line that declares them.
The old var keyword is hoisted and set to undefined, which hides mistakes. This is a good reason to avoid it.
Example
console.log(add(2, 3));
function add(a, b) {
return a + b;
}
try {
console.log(early);
let early = 1;
} catch (error) {
console.log(error.name);
}Exercise
Which kind of function can be called before its line?
greet() {}Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.