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;
}

Centering an Absolutely Positioned Element Using CSS

Centering an absolutely positioned element is a CSS challenge that I have always struggled with. I always find myself googling for the solution every now and then.

Today I came across this great article for exactly this problem and decided to share it with everybody and document it on my blog for easy referencing later!

Horizontally centering a static element in CSS is normally handled by setting the left and right margins to auto, for example:

#myelement
{
	margin: 0 auto;
}

However, this won’t work on an absolutely positioned element. Its location is determined in relation to the most immediate parent element that has a position of absolute, relative, or fixed.

In the following example, the relative red square has a width set to 40% of the available space. The top-left of the absolutely positioned blue square is positioned 30px across and 10px down:

#outer
{
	position: relative;
	width: 40%;
	height: 120px;
	margin: 20px auto;
	border: 2px solid #c00;
}  

#inner
{
	position: absolute;
	width: 100px;
	height: 100px;
	top: 10px;
	left: 30px;
	background-color: #00c;
}

If we’re unconcerned about the exact dimensions of our blue box, we could omit the width setting and set the same left and right values. This would effectively center our blue box:

#outer
{
	position: relative;
	width: 40%;
	height: 120px;
	margin: 20px auto;
	border: 2px solid #c00;
}  

#inner
{
	position: absolute;
	height: 100px;
	top: 10px;
	left: 30px;
	right: 30px;
	background-color: #00c;
}

So, how can we center our box if it has fixed dimensions? The answer requires a little lateral thinking:

  1. First, we use left: 50%. Unlike background image positions, this will move the left-hand edge of the blue box to the center.
  2. Since our box is too far to the right, we use a negative left margin that’s half its width. In our example, we must set margin-left to -50px to shift the box back to the right place:
#outer
{
	position: relative;
	width: 40%;
	height: 120px;
	margin: 20px auto;
	border: 2px solid #c00;
}  

#inner
{
	position: absolute;
	height: 100px;
        width: 100px;
	top: 10px;
	left: 50%;
	margin-left: -50px;
	background-color: #00c;
}

The blue box will remain centered no matter how the width of the outer element changes!

Via: SitePoint

FancyUpload – AJAX Uploader with Progress Bar

FancyUpload is a file-input replacement which features an unobtrusive, multiple-file selection menu and queued upload with an animated progress bar.

It is easy to setup, is server independent, completely styleable via CSS and XHTML and uses MooTools to work in all modern browsers.

It is fully compatible with all A-Grade Browsers (Internet Explorer 6+, Opera 9, Firefox 1.5+ and Safari 2+)

Features

  • Select and upload multiple files
  • Filter files by type in the select dialog
  • Optional Events to add your own behaviour
  • Show and filter useful file information before the upload starts
  • Limit uploads by file count and/or file size
  • Platform and server independent, just needs Flash 8+ (> 95% penetration)
  • Unobtrusive, since the element is replaced after the swf loaded successfully
  • Cancel running uploads, add files during upload
  • Everything is optional, documented and easy editable

[ Demo ] [ Website ]