• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaScript Hoisting?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all -

Could someone please better explain JavaScript Hoisting to me? I'm fairly new with JavaScript.

Thank you.

- Josh
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prior to ES6, JavaScript would "hoist" variable declarations to the function level. In other words, a variable declared anywhere in the body of the function acts like it had been declared at the top of the function.

Consider:

When the x function is executed, the browser displays an error such as:

Uncaught ReferenceError: z is not defined
   at x (<anonymous>:1:27)



Then consider:
As expected, there is no error, and because we did not define a value for z, the displayed value is undefined. Note that the value undefined, is not the same as undeclared -- the variable exists (is declared), but has no value (undefined).

Now consider:
You would think that the log statement would fail, but the value displayed is undefined because the variable declaration is "hoisted" to the top of the function, just like the previous example.

Note that:
Does not display 1, because even though the variable declaration is hoisted to the top of the function, the initialization still occurs after the log statement is executed.

As of ES6, JavaScript has block scoping rather than function scoping.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic