Hoisting in JavaScript means the interpreter moves all variable and function declarations to the top of their scope before running the code. For variables declared with var, this means they exist from the start but have the value undefined until assigned. For example, in the code, console.log(x) prints undefined before x is assigned 5 because only the declaration is hoisted, not the assignment. This is why the first console.log outputs undefined and the second outputs 5. Variables declared with let or const behave differently and cause errors if accessed before declaration. Understanding hoisting helps avoid confusion about variable values during code execution.