Advanced JS

js

Values

Scopes

Lexical scoping

When a function is defined in another function, the inner function has access to the outer function’s variables.

function outerFunction() {
  const outer = `I'm the outer function!`;

  function innerFunction() {
    const inner = `I'm the inner function!`;
    console.log(outer); // I'm the outer function!
  }
}

Closures

If you define any inner function within another function, this inner function is called a closure. It retains access to the variables created in the outer function.

function outerFunction() {
  const outer = `I see the outer variable!`;

  return function innerFunction() {
    console.log(outer); // I see the outer variable!
  };
}

Classes

Private fields

class Example {
  #priv = 0;
  getPriv() {
    return this.#priv;
  }
}

var vs let vs const

var:

let

const

Tagged templates

function template(strings, ...keys) { ... }

template`${0} ${'foo'}!`;

References

Other notes for js

< back to index