JavaScript 01: Vanilla JavaScript


Javascript multiline string

`x is a long
sentence in
one string`

TL;DR: backticks are awesome.


Javascript OR

||


Javascript: let vs const

let allows the variable to be reassigned multiple times.

const creates a variable that cannot be reassigned after it has been assigned a value.


Javascript: array basic tricks

Check if variable is of type Array.isArray()

Add Array to Array

Array forEach()

Array forEach() on Object keys

Array filter()

Array slice()

The spread syntax (...) and array methods such as .map(), .slice(), and .filter() can be used to immutably update the state of a complex app.


Objects

Object creation

Object: Add key-value pair

thought

Object: Create Shallow Copy/Clone

Object: Create Deep Copy/Clone

https://lodash.com/docs/#cloneDeep


Javascript: Promise vs Callback

A promise is when the wife asks you to do the dishes and you say "sure honey I'll do that"

A callback is when you tell her "hey honey I finished those dishes"

An await is when she asks you to do the dishes and she's standing there in the kitchen tapping her foot


What are the top 5 use cases for functions returning Promises?

Promises are the backbone of modern JavaScript.

  1. Fetching data (HTTP requests / APIs) - probably the most common. Functions that return Promises let you handle async network calls.

  2. Reading & writing files (Node.js) - I/O operations are async, so they return Promises.

  3. Database queries - Most DB libraries (MongoDB, PostgreSQL, etc.) return Promises for queries.

  4. Delays / timeouts - You can wrap setTimeout in a Promise to wait for something.

  5. Parallel / batch async tasks - Functions returning Promises can be run in parallel with Promise.all, Promise.race, etc.

When you have multiple async operations that don’t depend on each other, you can run them at the same time instead of one after the other.

  1. User interactions (events wrapped in Promises) - Sometimes you want to wait for a user action just once (e.g., button click, modal close).

Leaving a listener attached is sometimes okay, but wrapping it in a Promise:

  • Automatically removes the listener after the first event.

  • Prevents memory leaks.

  • Makes async code cleaner (await).

  • Lets you integrate it with other Promises (Promise.all, Promise.race).

  1. Animations & transitions - You can wrap CSS/JS animations in Promises so they integrate with async code.

  2. Geolocation & browser APIs - Many browser APIs (Clipboard, Geolocation, Notifications, etc.) use Promises.

  3. Background jobs / workers - Web Workers or background tasks in Node often expose Promises.

For heavy tasks (image processing, data crunching, machine learning, etc.), you don’t want to freeze the main UI thread.

That’s what Web Workers (browser) or Worker Threads (Node.js) are for. They run in the background.

  1. Retries, polling, & error handling - Functions that retry failed requests or poll for changes often return Promises.

  2. Payment APIs – Stripe, PayPal, etc. return Promises for charges and tokens.

  3. Authentication – login, logout, token refresh.

  4. Hardware access – WebUSB, WebBluetooth, WebRTC calls.

  5. Testing async code – most test runners accept functions returning Promises.

  6. Build tooling – webpack, rollup, ESLint plugins often return Promises.

  7. CI/CD scripts – deploy steps, running shell commands asynchronously.

explain further 5, 6, 9


How do you return a Promise with a function?

Two options

  1. Explicitly return a Promise

  1. Make the function async using async keyword

Even though it looks like it’s just returning a string, the function is async.

That means JavaScript automatically wraps the return value in a Promise.resolve(...).

  1. With await inside async


Javascript: Iterate Over Object

https://stackoverflow.com/a/43392879/759452


Javascript: ...


Javascript: Switch Case


JS: Optional Chaining

The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.

becomes


JS: Optional Chaining For Method Calls

Optional Chaining for method calls can be much nicer for deeply-nested methods:

vs:

vs:


JS: Destructuring: Nested object and array

Example 1

Example 2


Backticks

ES6 feature, called template literals.

Use Cases:

  • interpolate values into strings dynamically

  • JSX syntax to inject values dynamically into the render method of the component

  • allowed to split across multiple lines

Notice the extra $ Sign:


Web Storage API: sessionStorage and localStorage

Web Storage API offers 2 mechanisms: sessionStorage and localStorage.

Allows to save data as key-value pairs in the browser for later use.

Data stored here will always be available to your Javascript code and cannot be accessed from the backend. Thus you will have to manually add it to your requests in a header for example.

This storage is only available to your app's domain and not to sub domains.

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


localStorage vs sessionStorage

Main difference is in data expiry:

  • sessionStorage: Data available only for a session (until the browser or tab is closed).

  • localStorage: Stores data with no expiration date, does not clear data when the browser closes; only gets cleared through JavaScript, or clearing the Browser cache/Locally Stored Data


localStorage

Lifecycle: localStorage does not clear data when the browser closes.

Ideal for: persisting data not bound to the current browser tab.

UC: dark mode feature, persist to-do items, or persist a user’s form input values

How To:

  1. localStorage allows you to store only string values

  2. store object data to localStorage, convert it to a string - serialize it w JSON.stringify()

  3. get object data from localStorage, convert string back to object data - deserialize it w JSON.parse()

Sources:

  • https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

  • https://blog.logrocket.com/storing-retrieving-javascript-objects-localstorage/

  • localStorage+react https://blog.logrocket.com/using-localstorage-react-hooks/


Cookies

TODO


How do we send cookies from the browser to the server?

read cookies with javascript and set the request header?

TODO


How do we send cookies from the server to the browser?

set the response header?

TODO


Event Syntax Equivalents


functions


functions: one-liner arrow functions


functions: embedded one-liner arrow functions


functions: arrow functions multi-liners

Rule: just like regular functions, add curly braces and optionally add return

—— functions: return ——


Self-Executing Anonymous Functions


Primitive vs Reference Type

https://www.youtube.com/watch?v=9ooYYRLdg_g


Last updated