What is Hoisting in JavaScript?

An unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception.

This concept is known as “hoisting”.

Variables in JavaScript are, in a sense, “hoisted” (or”lifted”) to the top of the function or statement.

However, variables that are hoisted return a value of undefined.

Because of hoisting, all var statements in a function should be placed as near to the top of the function as possible.

This best practice increases the clarity of the code.

In the case of functions, only function declarations are hoisted-but not the function expressions.

Leave a Reply

Your email address will not be published. Required fields are marked *