JavaScript

https://developer.mozilla.org/en-US/docs/Web/JavaScript

  • Recognized for
    • First-class functions
    • Weak typing
    • Prototypes
    • Automatic semicolon comp.

History

Developed by Brenden Eich of Netscape, initially named Mocha, then LiveScript, and finally JavaScript.

Variables

naming convention: camel case.

  • var (the old way and should be avoided)
    • Global or function scope (due to hoisting)
    • Hoisted, declared, and not initialized, causing unexpected behaviors.
    • Redeclaration allowed (old value maintained unless assigned), causing confusion
    • Should be avoided since ES6
  • let
    • Block scope
    • Redeclaration not allowed, reassignment allowed
  • const
    • Block scope
    • Redeclaration and reassignment not allowed
  • Without keyword
    • Will be in global scope (even if inside a block)
  • Hoisting
    • = Raise something up with ropes and pulleys
    • Move the declaration up (but not the initialization)
console.log(x); // will output undefined
var x = 7;
  • ReferenceError vs undefined
  • When entering a function, JS will search and execute all the vars and declare them all.

Operators

  • == returns true for “same thing”, === also requires “same type”. Always use === unless == is intended. (type coercion)

Functions

function func() { ... };
const func = function () { ... };
const func () => { ... };  // since ES6

All functions are hoisted.

Data Types

Primitives

  • Boolean
  • Number
  • BigInt - const x = 111111n
  • String - double quote, single quote, backtick (template string, since es6)
  • Undefined type
  • Symbol type

Objects

  • Prototype-based object-oriented programming
  • person.name and person["name"] both works
  • Context: this.name
  • const func = person.method.bind(person). Calling func = person.method will cause many undefined, since