Categories
JavaScript

Modern JavaScript: ES6 Basics

Task 1: Introduction

  1. Rhyme: cloud learning system, splite screen to workspace(desktop) and video;
  2. nodemon -> browser-sync;
  3. npx: execute bin file as node dependencies.

Task 2: Variable (let) and Scoping

var:

  • redeclarable
  • only function-scope, but no block-scope: var defined inside a for-loop could be used outside the loop.
aconsole.log(hi);ReferenceError: hi is not defined;
bvar hi;
console.log(hi);
undefined;
cconsole.log(hi);
var hi;
undefined;
1. variables and functions in JS are hoisted;
2. var are initialized to undefined;

let:

  • not redeclarable
  • both function-scope and block-scope
aconsole.log(hi);ReferenceError: hi is not defined;
blet hi;
console.log(hi);
undefined;
cconsole.log(hi);
let hi;
ReferenceError: hi is not defined;
hoisted but not initialized as undefined(?);
  • Hoisting in JS: [Ref: MDN Hoisting]
    1. A strict definition of hoisting suggests that variable and function declarations are physically moved to the top of your code.
    2. JavaScript only hoists declarations, not initializations. If a variable is declared and initialized after using it, the value will be undefined.

Leave a comment