JavaScript Flashcards
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
- Global - created when a JavaScript script first starts to run, and it represents the global scope in JavaScript.
- Function - created whenever a function is called, representing the function’s local scope
Two phases of the Execution Context
- Creation phase: the JavaScript engine creates the execution context and sets up the script’s environment. It determines the values of variables and functions and sets up the scope chain for the execution context.
- Execution phase: In this phase, the JavaScript engine executes the code in the execution context. It processes any statements or expressions in the script and evaluates any function calls.
Two parts of Execution Context
- Thread of execution
- Memory
JS Call Stack
- In a JS call stack, the elements are function invocations. Every time you invoke a function, it’s added to the stack. If it has a nested function, that nested function will get added to the top of the stack as well, and so on.
- responsible for keeping track of all the operations in line to be executed. Whenever a function is finished, it is popped from the stack.
- You can think of the call stack as a to-do list of sorts for JavaScript.
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
- Block scope
- Function scope
- Global scope
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
- lets you store unique values of any type
- A value in the set may only occur once
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
- scope is function-based (access to variables)
- context is object-based (value of this)
Javascript runtime environment
- where your javascript code is executed when you run it.
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
- Open Source, written in C++
- Used in Chrome and Node.js
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
- an event-handling pattern (based on event bubbling) that allows you to handle events at a higher level in the DOM tree other than the level where the event was first received.
Event capturing
- event propagates from the outermost element to the target element.
- opposite of event bubbling, where events propagate outwards from the target to the outer elements.
3 phases of Event Propogation
- Capture phase - the event starts from Window and moves down to Document, the root element and through ancestors of the target element.
- Target phase, the event gets triggered on the event target (e.g. the button the user clicked).
- Bubble phase, the event bubbles up through ancestors of the target element until the root element, Document and, finally, Window.
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
generator
JS
a process that can be paused and resumed and can yield multiple values.
A generator in JavaScript consists of a generator function, which returns an iterable Generator object.
// Generator function declaration
function* generatorFunction() {}
https://www.digitalocean.com/community/tutorials/understanding-generators-in-javascript
Iteration protocols
There are two iteration protocols:
1. iterator protocol
2. iterable protocol
Iterable protocol
allows JS objects to define or customize their iteration behavior, such as what values are looped over in a for…of construct. Some built-in types are built-in iterables with a default iteration behavior, such as Array or Map, while other types (such as Object) are not.
In order to be iterable, an object must implement the @@iterator method, meaning that the object (or one of the objects up its prototype chain) must have a property with a @@iterator key which is available via constant Symbol.iterator:
Iterator
- Any object that implements the “Iterator Protocol”,
- aka –> it has a next() method that returns an object with two properties:
1. done: (Bool)
2. value: the current element.
Explain the difference between cookies, session storage, and local storage?
JavaScript provides three mechanisms for storing data on the client − cookies, session storage, and local storage. Each one has advantages and disadvantages.
- Local storage is the most recent mechanism. It allows for larger amounts of data to be stored, but the data is not deleted when the browser is closed. Local storage is useful for storing data that the user will need to access later, such as offline data.
- Session storage is similar to cookies, but the data is only stored for the current session. This means that the data will be deleted when the user closes the browser. Session storage is useful for storing data that is sensitive, such as login credentials.
- Cookies are the oldest and most well-known mechanism. They are simple to use and well supported by browsers. However, they are limited to 4KB of data and are often used to store data that is not sensitive, such as user preferences.
Does an empty array in JS evaluate to truthy or falsy?
truthy