Web APIs
aka Browser APIs
APIs that are built into browser
e.g. fetch, setTimeout
??
nullish coalescing operator
const foo = null ?? 'default string'; console.log(foo); // Expected output: "default string" const baz = 0 ?? 42; console.log(baz); // Expected output: 0
returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
useful if left-side value is 0 and we want to use that value
Two types of Execution Context
Two phases of the Execution Context
Two parts of Execution Context
JS Call Stack
LIFO
JS Event Loop
constantly checks whether or not the call stack is empty. If it is empty, new functions are added from the event queue. If it is not, then the current function call is processed.
JS Microtask Queue
* Queue for cb funcs from promises
* Higher Priority than Callback Queue
* Microtask Queue is like the Callback Queue, but Microtask Queue has higher priority. All the callback functions coming through Promises and Mutation Observer will go inside the Microtask Queue. For example, in the case of fetch(), the callback function gets to the Microtask Queue. Promise handling always has higher priority so the JavaScript engine executes all the tasks from Microtask Queue and then moves to the Callback Queue.
What is Scope in JS
determines what variables you have access to
JS dot notation vs bracket notation
Bracket notation, unlike dot notation, can be used with variables.
const myObj = {}
const newNumKeyToAdd = 13
myObj[newNumKeyToAdd] = 1
// {13: 1}you cant do that with dot notation
Set
JS
const mySet1 = new Set() mySet1.add(1) mySet1.add(5) mySet1.add(5) mySet1.size; // 2 mySet1.has(1); // true mySet1.delete(5); // removes 5 from the set
Scope vs. Context
JS
Javascript runtime environment
JavaScript code may be executed in one of two runtime environments:
1. a browser’s runtime environment
2. the Node runtime environment
Examples of what JS Engines are used in different runtime environments:
* Chrome: V8
* node: V8
* Mozilla: Spidermonkey
* IE: Chakra
V8
Google’s JS & WebAssembly engine
Event bubbling
when an element receives an event (e.g. click, scroll, etc…), and that event bubbles up (aka transmitted/propagated) to its parent and ancestor elements in the DOM tree until it gets to the root element.
Event delegation
Event capturing
3 phases of Event Propogation
Explain how this works
Case 1: Default - In a regular function (or if you’re not in a function at all), this points to window. This is the default case.
Case 2: Left of the dot - When a function is called as a method, this points to the object that’s on the left side of the dot.
Case 3: Constructor - In a function that’s being called as a constructor, this points to the object that the constructor is creating.
Case 4: Explicitly set - When you explicitly set the value of this manually using bind, apply, or call, it’s all up to you.
Case 5: Callback - In a callback function, apply the above rules methodically.
ES6 Template Literals
`Welcome ${firstName}, ${lastName}!`bind()
returns a new function that when invoked, has its this set to a specific value
call()
functionName.call(thisArg, arg1, arg2, ...);
calls a function with this explicitly set
apply()
fn.apply(thisArg, [args]);
same as call() except it takes the args as an array
prototype chain / prototypal inheritance
JS
Every object in JavaScript has a built-in property, which is called its prototype.
The prototype is itself an object, so the prototype will have its own prototype, making what’s called a prototype chain.
The chain ends when we reach a prototype that has null for its own prototype.
prototypal inheritance - When we read a property from an object, and it’s missing, JavaScript will check if the prototype has that property and use it if it does