What is Variable Hoisting in JavaScript?

Consider the following code:

1
2
var sheldon = 'bazinga!';
alert(sheldon); // bazinga!

Okay, of course the alert will display “bazinga!.” That’s obvious; however, stick with me.

Let’s next create an immediate function, which alerts the same value.

1
2
3
4
5
var sheldon = 'bazinga!';
 
(function() {
  alert(sheldon); // bazinga!
})();

All right, all right. Still obvious, I know. Now, let’s throw a wrench into the mix, and create a local variable within this anonymous function of the same name.

1
2
3
4
5
6
  var sheldon = 'bazinga!';
 
(function() {
  alert(sheldon); // undefined
  var sheldon = 'local bazinga!';
})();

Huh? Why is the alert now displaying undefined? Even though we’ve declared a new variable, it’s still below the alert; so it shouldn’t have an effect, right? Wrong.

Variable Declarations are Hoisted

Within its current scope, regardless of where a variable is declared, it will be, behind the scenes, hoisted to the top. However, only the declaration will be hoisted. If the variable is also initialized, the current value, at the top of the scope, will initially be set to undefined.

Okay, let’s decipher the difference between the terms, declaration andinitialization. Assume the following line: var joe = 'plumber';

Declaration
1
var joe; // the declaration
Initialization
1
joe = 'plumber'; // the initialization

Now that we understand the terminology, we can more easily comprehend what’s happening under the hood. Consider the following bogus function.

1
2
3
4
5
6
7
8
(function() {
  var a = 'a';
  // lines of code
  var b = 'b';
  // more lines of code
  var c= 'c'; // antipattern
  // final lines of scripting
})();

Declare all variables at the top.

Note that what’s exemplified above is considered to be bad practice. Nonetheless, behind the scenes, all of those variable declarations — regardless of where they occur in the function scope — will be hoisted to the top, like so:

1
2
3
4
5
6
7
8
9
(function() {
  var a, b, c; // variables declared
  a = 'a';
  // lines of code
  b = 'b'; // initialized
  // more lines of code
  c= 'c'; // initialized
  // final lines of scripting
})();

Aha Moment

If we now return to the original confusing undefined piece of code, from above:

1
2
3
4
5
6
var sheldon = 'bazinga!';
 
(function() {
  alert(sheldon); // undefined
  var sheldon = 'local bazinga!';
})();

It should now make perfect sense why sheldon is alerting undefined. As we learned, as soon as the local variable, sheldon, was declared, it was automatically hoisted to the top of the function scope…above the alert. As a result, the variable had already been declared at the time of the alert; however, because initializations aren’t hoisted as well, the value of the variable is: undefined.

Summary

To put in simple words, function declarations and variable declarations are always moved (“hoisted”) invisibly to the top of their containing scope by the JavaScript interpreter. This means that code like this:


function foo() {
bar();
var x = 1;
}

is actually interpreted like this:

function foo() {
var x;
bar();
x = 1;
}

World War Z – Day 3 of filming in Glasgow

The actual filming of the upcoming Brad Pitt’s movie “World War Z” has finally started today. While passing by the George Square in the morning (enroute to Office!), I saw all the extras had been assembled on the Cochrane street and it was quite crowded.
The atmosphere was looking more lively than ever and there was big entourage of American cars/taxis on the street. It really felt like an American city center for a change! (in contrast to mostly quiet and less crowded streets of Glasgow)

I will try to update the post later in the evening today, (once am back from the work) with most recent pics from day 3. So stay tuned!

P.S. You can catch the live action of filming on this webcam

World War Z – Day 2 of filming in Glasgow

Today was the second day of filming WWZ in Glasgow city center. Shooting has not started yet. Preliminary preparations for the shoot are still underway. Things have started to take shape with the gradual Americanization of the Scottish city.

American flags were placed atop some of the main buildings in George sq.

Corinthian Club adorns American flag

Army jeeps have also been brought in today to be used in the shoot.

Army Jeeps

Army Jeeps

Army Jeeps

Brad Pitt and Angelina Jolie along with their kids arrived to Glasgow today via Virgin Train. They reportedly hired the entire train to transport their family to Glasgow for filming of World War Z. Actual filming is expected to commence from tomorrow.

Below are some other random pics from the Day 2. Enjoy!

Hotdog stall

Philadelphia City Paper

Coffee street stall

Crew Vans

Americans Cars/Taxis lined up for the shoot

American Cop cars

SWAT Van

Scottish Postbox concealed!

United States Court House

United States Court House

Americanish bus?

Crew installing the American-styled traffic lights

World War Z filming kicks off in Glasgow

Today was the first day of filming for Brad Pitt’s new  zombie film, ‘World War Z’, in Glasgow. The city center will be transformed into a post apocalyptic war-torn ruin. Glasgow will double for the US city of Philadelphia, with false shop fronts being constructed and American cars on the roads.

The film crew were busy transforming the George Square into Philadelphia today and all the roads were barricaded with no access to traffic. The actual shooting is supposed to start from Wednesday once everything is in place. I took some quick snaps and couple of videos of the film location, while coming back from work. Will keep on updating the blog about any latest developments, photos and vids relating to World War Z in Glasgow.

Google Takeout – Download your Data from all Google Services at Once

Google recently introduced Google Takeout, a new service which helps you to download all your data hosted on Google which includes your Picasa albums, Google contacts, your Buzz updates and even your Google+ updates.

The downloaded data is useful in many ways, for example, all the photos uploaded on the Picasa, in every album can be dowloaded in one go. The downloaded albums are arranged in folders.

  1. Head on to Google Takeout and sign in to your Google account.
  2. Under the Choose services tab you can choose what all data you want to download, you will also get the estimated size of the files.
  3. After that, click on Downloads tab and hit Download (you need to re-login).
However, the data from Docs, YouTube, Orkut etc cannot be downloaded using Google Takeout but there’s another service by known as Data Liberation which gives step-by step method to download your data from each Google service.