Skip to content

Instantly share code, notes, and snippets.

@joey2031
Created January 21, 2019 18:11
Show Gist options
  • Save joey2031/424a9d51056471cb2fa9725aa2f5932b to your computer and use it in GitHub Desktop.
Save joey2031/424a9d51056471cb2fa9725aa2f5932b to your computer and use it in GitHub Desktop.
Hoisting in javascript created by joey2031 - https://repl.it/@joey2031/Hoisting-in-javascript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script src="script.js"></script>
</body>
</html>
var x;
var y = 7;
console.log("The value of x is " + x); // prints undefied
console.log(`The value of y is ${y}`); // prints 7
// prints undefied this is called hoisting, it brings the var z(the declraion) but not the assignment of 7 to the top. This is usless we do not want this.
console.log(`The value of z is ${z}`);
var z = 7;
// This is why let is better then var because it does not hoist the variables.
let a = 90;
let w;
console.log(`the value of w is ${w}`);
console.log(`The value of a is ${a}`); // prints 90
console.log(`The value of b is ${b}`); //this will not print, instead it gives you a run time error. Also anything you put after this line will not execute because of the run time error.
let b = 70;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment