Skip to content

Instantly share code, notes, and snippets.

@adeelibr
Created November 24, 2019 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adeelibr/c764d9ca1e2d12b5f41407eb5a80d893 to your computer and use it in GitHub Desktop.
Save adeelibr/c764d9ca1e2d12b5f41407eb5a80d893 to your computer and use it in GitHub Desktop.
hoisting.js
// ### EXAMPLE 1
// -- what you type
// var name = 'adeel';
// var profession = 'developer';
// console.log(name + ' ' + profession);
// -- what the machine does
// var name; // undefined
// var profession; // undefined;
// name = 'adeel';
// profession = 'developer';
// console.log(name + ' ' + profession);
// ### EXAMPLE 2
// -- what you type
// a = 2;
// var a;
// console.log( a );
// -- what the machine does
// var a;
// a = 2;
// console.log(a);
// ### EXAMPLE 3
// -- what you type
// console.log( a );
// var a = 2;
// -- what the machine does
// var a; // undefined;
// console.log(a);
// a = 2;
// ### EXAMPLE 4
// -- what you type
// foo();
// function foo() {
// console.log( a ); // undefined
// var a = 2;
// }
// var result = 1;
// console.log(result);
// -- what the machine does;
// function foo() {
// var a; // undefined;
// console.log(a);
// a = 2;
// }
// foo();
// var result;
// result = 1;
// console.log(result);
// ### EXAMPLE 5
// -- what you type
// foo();
// var foo = function bar() {
// console.log('hello');
// };
// what the machine does;
// var foo; // undefined;
// foo(); // not ReferenceError, but TypeError!
// foo = function bar() {
// console.log('hello');
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment