JavaScript overview

High-Level

Low-level languages like C need a dev to manage resources manually. JS is high-level so does not have to worry as system resource management happens automatically (abstractions).

+ easy to use and learn
– not as fast or optimised

Garbage-collected

Automatically clears unused objects from memory

Interpreted or Just-in-time compiled

ones and zeros (machine-code) via compiling / interpretation via the javascript engine. The engine compiles the source code to machine code and then immediately executes it without putting it in a portable file. Faster than compiling it line by line.

STEPS:

  • Parsing (AST) goes through the code and turns it into a tree
  • Compilation – turns it into machine code
  • Execution – (happens in the call stack) – Code is reoptimised during execution.

Multi-paradigm

More than one way to skin a cat. Imperative / declarative.

  • Procedural programming (linear)
  • Object-oriented programming (OOP)
  • Functional programming (FP)

Prototype-based object-oriented

Template/blueprint.

First-class functions

Functions used as variables. Can be passed to other functions.

Dynamic

Dynamically typed. Variable datatypes not assigned manually, js assigns them. A bit buggy – Typescript resolves

Non-blocking event loop

Concurrency model – JS engine handles multiples tasks at the same time although runs in one single thread. Event loops avoid long running tasks blocking.

JS ENGINE

Engine executes JS code. All browsers have their own engines. Chrome uses V8 (also powers node JS) for example.

All engines have a call stack and a heap.

CALL STACK
Is where code is executed.

HEAP
Is where objects are stored.

Javascript runtime in the browser

Runtime = is everything in the browser required to run javascript: engine, web APIs (accessed through global window), callback queue (click, timer, data)

Execution Concept

  • Variable environment
  • scope chain
  • this keyword

3 types of Scope

GLOBAL, FUNCTION, BLOCK (ES6)

Global Scope

These variables are declared outside of function and block and are accessible anywhere.

Function Scope (local scope)

These variables only accessible inside function.

Block Scope

Everything within curly braces (if or while loops). Only let or const variables (not var wehich is function-scoped). Functions are block scoped (in strict mode)

Scope Chain

Variable Lookup. A scope has access to variable from all outer (parent) scopes.

Hoisting

Hoisting makes some types of variables accessible/ usable in the code before they are actually declared (lifted to the top of the scope)

Hoisted: function dec & var, function expressions and arrows (depends on declared with let or const)

TEMPORAL DEAD ZONE

Leave a Reply

Your email address will not be published. Required fields are marked *