Skip to content

JavaScript

Strings

Template Literals

Feels like Python f"formatted_string"

myTag`A${1}B${2}C${3}D${4}E${5}F`;

function myTag(strings, ...args) {
    console.log(strings); // ['A', 'B', 'C', 'D', 'E', 'F']
    console.log(args); // [1,2,3,4,5]
    return strings;
}

Numbers

NaN

Number.isNaN("a"); // false
isNaN("a"); // true

Number.isNaN(Number(input)) === isNan(input);

Functions

kwargs (obj --> params)

const sum = (a, b) => a + b;

How do I pass this in args = {a: 1, "1": 2}?

Can't destructure

// sum(args)  // a = args, b = undefined
// sum(...args)  // TypeError: Found non-callable @@iterator

Solution: get the object values as an array

// order: [Does JavaScript guarantee object property order?](https://stackoverflow.com/a/23202095)
// 1. integer keys (like "11")
// 2. string keys
// 3. Symbol keys
argValues = Object.values(args); // [2,1]
c = sum(...argValues);
console.log(c);

Importing

CommonJS vs AMD

at the start, commonJS sucked on browsers

AMD was used because people had to use it

you explicitly declare your dependencies for each function/module

now, neither are the best

Use a build tool

Import a package in the dev tools

Dynamic imports

const { default: Confetti } = await import('https://cdn.skypack.dev/canvas-confetti');

Last update: 2022-11-04