JavaScript Basic Syntax

What is JavaScript?

A language for manipulating the Browser

  • Of the browser

  • By the browser

  • For the browser

JavaScript is a war with standards!

JQuery

  • Using JQuery may slow things down slightly, but it eliminates cross-browsing issues

Vanilla JS

: A campaign saying let's stop using the JavaScript Libraries popping up everywhere!

  • Vanilla == 'pure/original'

  • Using JavaScript without installing any library!

JavaScript

window is at the top

  • window.document

Writing

  • window.document.write()

Since the top is always window, it can be omitted

ASI (Automatic Semicolon Insertion)

: The principle of the feature is to provide a little leniency when evaluating the syntax of a JavaScript program by conceptually inserting missing semicolons

3 Core Components

  1. BOM (Browser Object Model)

  2. DOM (Document Object Model)

    • DOM manipulation

    • The reason JavaScript exists

  3. ES (EcmaScript)

ES (EcmaScript)

let vs const

let

: Reassignment is possible

const

: Reassignment is not possible !== the value doesn't change

Naming Convention

  • Use lower camelCase!

  • No snake_case!

Never use loose equality operators

  • ==, != => X

  • ===, !== => O

Function => First-class Object

  1. Can be stored in a variable.

  2. Can be a return value of a function.

  3. Can be an argument to a function.

DOM (Document Object Model) Manipulation

  • Convert String-based HTML to Objects

  • Understand and interpret

  • Build a DOM Tree

  • Render on screen (draw)

Summary:

  • All data in the world is String (HTML)

  • It must go through a parse/parsing process to be interpreted (DOM interprets)

Selecting

: querySelector , querySelectorAll

Create

: .classList.add()


I. Scope

The youngest sibling shut-in rule

  • When there are curly braces, a Scope is created!!

    1. In case of a function

    2. In case of control statements (if / while / switch (control)), etc.

  • var was only scoped within functions

  • let works the same way when there are curly braces (scope)

II. var, let, const

  • var

    • Free assignment

    • Free declaration

    • Function scope (never use this.)

  • let

    • Free assignment

    • Can only be declared once

    • Block scope

  • const

    • Assign once

    • Declare once

    • Block scope

Identifier Convention

  • Variables, functions, objects: camelCase (camelCase)

  • Classes, constructors: PascalCase (PascalCase)

  • Constants: uppercase snake case (SNAKE_CASE)

III. Hoisting

  1. Don't use var

  2. Don't use function declaration form

  • Using declaration form makes code not work as expected

    • Why?

      • It scans through once before execution, making references possible before declaration

V. Data Types

(1) Numbers

(2) Strings

  • Concatenation

    • +

  • Interpolation

    • template literal ${}

(3) Boolean

  • What is close to true and what is close to false

  • true, false (lowercase)

1. Truthy

All values that are not Falsy

  • [] - emptyArray

  • {} - emptyObject

2. Falsy

Non-existent or undefined things

  • null

  • undefined

  • ""

  • 0

  • [] == true

  • ![]

VI. Data Structures

How to store and manipulate (CRUD) => look at methods

  • Array (list)

    • array helper method

  • Object (dict)

VII. Functions

  • Declaration (statement)

  • Expression

Arrow Function

Use only where it is NOT a (Class) Method(OOP) function

  1. Don't use arrow functions in Class definitions!

    • Why?

      • Because they don't bind this, arguments, super, new.target, etc.

  2. Cannot be used as a constructor X

  3. Cannot be used as an Event Listener Callback function X

ex)

VIII. OOP

  • Very flexible OOP through objects

  • Forget about Classes for now!

    • Prototypal Inheritance

  • class introduced from ES6+

ex)

Last updated