Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function helloFunction() {
- console.log(hello);
- var hello = "e";
- }
- // This will work because of hoisting. The preprocessor will move up the var/let statement while compiling, like this:
- function helloFunction() {
- var hello;
- console.log(hello);
- hello = "e";
- }
- // The difference between let and var is where they are hoisted. let hoists up to the last {, var hoists up to the last function begin.
- // See those functions?
- function varHello() {
- try {
- console.log(hello);
- var hello = "e";
- } catch {
- console.error("Error!");
- }
- }
- function letHello() {
- try {
- console.log(hello);
- let hello = "e";
- } catch {
- console.error("Error!");
- }
- }
- // That's how they'll be preprocessed:
- function varHello() {
- var hello;
- try {
- console.log(hello);
- hello = "e";
- } catch {
- console.error("Error!");
- }
- delete hello;
- }
- function letHello() {
- try {
- let hello;
- console.log(hello);
- hello = "e";
- delete hello;
- } catch {
- console.error("Error!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment