Predict var Hoisting
Predict Output Medium
Question
What does this code print?
Code
console.log(x);
var x = 5;
console.log(x);
Hint
var declarations are hoisted, but assignments are not.
Explanation
var x is hoisted to the top, so x exists but is undefined before the assignment line. The first console.log(x) prints undefined. Then x = 5 runs, and the second console.log(x) prints 5.
Related Resources
Related Lessons
- JavaScript Variables
Learn about var hoisting.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.