Tuesday, February 25, 2014

JavaScript Hoisting

JavaScript lets you declare and initialize variables at any location within a function. You can even reference a function variable before you reach the variable declaration – How is that possible?

JavaScript does some work behind the scenes to bring all variable “declarations” to the top of a function. This process of automatically moving the variable declaration to the top is known as “hoisting”.

Variable declaration and assignment are two different things. JavaScript only hoists the declaration and assigns “undefined”.

Moral of the story – Always declare and initialize function variables at the beginning of JavaScript functions.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.