This article is still in development. Please be patient.

# Introduction

A friend recently asked me to explain why the following code did not work as expected:

var x = 1;
console.log(x); //expected output: 1
if(x===1){
    var x = 2;
    console.log(x); //expected output: 2
}
console.log(x); //expected output: 1

I knew immediately that it had to do with the way javascript handles the var keyword and hoists the declaration. I wanted to explain a little better, so I referred to the docs at Mozilla Developer Network and found a few more interesting things to look out for.

# What is Hoisting?