Introduction
- codr • Learn JS by solving free coding puzzles. See this 5 minute video by Basarat Ali for a tour. 🧑🎓
- Execute Program • Gary Bernhardt’s interactive programming courses 🧑🎓
- The Birth & Death of JavaScript • Gary Bernhardt 📺
- JavaScript Data Structures: Getting Started • Academind 📺
- JavaScript on Exercism • Get fluent in JavaScript by solving 141 exercises • Exercism 👩🎓
Functions
function
keyword vs variable assignment:- one reason defining functions using the function keyword (instead of with variable assignment) can sometimes be preferable: the former are hoisted to the top of the file automatically so you can define them below the code that calls them if you want to
- Sometimes I want to put helpers further down the file (like footnotes) so the main export can sit loud and proud at the top of a file
- Can define anywhere in file, since hoisted (not necessarily above the line that uses it)
- All optional aesthetics, though
Arrays
- 2 Ways to Sort an Array of Objects by Property Value • Sling Academy 📖
- 📺 Reduce: 10 Different Examples • 25 minute video by Leigh Halliday about how to use
Array.reduce()
to process arrays in various ways - 📺 Can you solve this tricky JavaScript Array problem? • 20 minute video by Wes Bos
Generating an array of values that follow a pattern
- e.g.
Array.from({ length: 21 }, (_, i) => i * 0.05)
generates an array of numbers from 0 to 1 in increments of 0.05 - Array.from() • MDN 📚
Filtering an array with an async callback
- Basically, you can’t do it directly because an async function returns a promise, which is always truthy
- Several workarounds
- Simplest is to map, then filter
- JavaScript Array filter with async/await • Stack Overflow 👨💻
- How to use async functions with Array.filter in Javascript • Tamás Sallai 📖
Loop n times
Array.from(Array(10).keys()).forEach(() => {
createBookNode(createNode, { ...dummyBookNode, id: shortid.generate() })
createAlbumNode(createNode, { ...dummyAlbumNode, id: shortid.generate() })
createPodcastNode(createNode, { ...dummyPodcastNode, id: shortid.generate() })
})
or
Array.from({ length: 21 }, (_, i) => i * 0.05);
Links
- generate an array of numbers • Stack Overflow 👨💻
Arrays & Objects
- 📺 JS Destructuring in 100 Seconds • 4 minute video by Fireship showing how to destructure JavaScript arrays and objects
Objects
- Deep Cloning Objects in JavaScript, the Modern Way • It’s been a long time coming, but we finally now have the built-in
structuredClone
function to make deep cloning objects in JavaScript a breeze
Referencing an object’s own properties
Simple way:
const obj = {
a: value => value || obj.b,
b: ‘something’
}
obj.a(true) // true
obj.a(false) // ‘something’
JS has late binding, so obj doesn’t need to be initialized before it is referenced. Be very careful with the way you leverage the power of lexical scoping of mutable references. In this case you are ok because of the use of const, but scary things can happen if you use let or something similar.
More involved way, but safer if not using const:
const createObj = () => {
const b = ‘something’
return {
a: value => value || b,
b: b
}
}
const obj = createObj()
obj.a(true) // true
obj.a(false) // ‘something’
An inlined anonymous function is safer since it limits the number of locations that can mutate the obj reference.
Links:
- Object referencing its own property on initialization • Stack Overflow 👨💻
Promises
- 📺 JS Fundamentals: Promise.all / Promise.any • 9 minute video by Leigh Halliday about how
Promise.all()
andPromise.any()
can help when multiple promises are pending at the same time - JavaScript Promise: .all() vs .allSettled() and .race() vs .any() - DEV Community
- JavaScript waitFor Polling • As more of the JavaScript developers write becomes asynchronous, it’s only natural to need to wait for conditions to be met…
Limiting a number between a minimum and maximum value
- How can I use JavaScript to limit a number between a min/max value? • Stack Overflow 👨💻
- Did this to limit the size of some text (min 3px, max 20px)
- Also,
lodash/clamp
JSDoc
- Use JSDoc: Index • Official documentation for JSDoc 3
Debugging
- Chrome Dev Tools:
- Debugger:
- Debugging JavaScript - Chrome DevTools 101 • Google Chrome Developers demonstrates how to use the Chrome Dev Tools debugger to step through code and inspect variables 📺
- Use XHR/fetch Breakpoints! • David Walsh explains how to use XHR/fetch breakpoints to debug AJAX requests in the Chrome Dev Tools debugger 📖
- Working with the Debugger • Harry Cresswell 📖
- Debugging Node.js, The Right Way • Builder.io 📖
- Performance tab:
- Use the Performance tab to record a page load and see what’s taking the most time:
- Sort by “Self Time” to see which functions are taking the most time to run
- “How to find your slowest lines of code using Chrome’s Performance tab”
- Sort by “Network” to see which resources are taking the most time to load (true?)
- Sort by “Self Time” to see which functions are taking the most time to run
- Use the Performance tab to record a page load and see what’s taking the most time:
- Debugger:
- Safari Dev Tools:
- Debugger:
- Debugging iOS Safari JS issues on a mac • Wes Bos demonstrates how to use the macOS Safari Dev Tools to debug an issue happening in iOS Safari 📺
- Debugger:
Bun
- Bun is disrupting JavaScript land • Fireship 📺
maybeString ?? ''
vs String(maybeString)
vs maybeString.toString()
String(maybeString)
— was my default, and works great for converting numbers and booleans to their string versions; but has the problem of turning null and undefined into the literal strings “null” and “undefined”. that’s never what i wantmaybeString ?? ''
— generally what I want for a string | null/undefined case; if null/undefined, give me an empty string so I’m still passing the expected type (a string)maybeString.toString()
- if you want to convert numbers, booleans, etc to strings, but throw an error on null/undefined instead of allowing a backup value (like an empty string)
Inbox
- https://gomakethings.com/simpler-boolean-returns-in-functions-with-javascript/
- https://kilianvalkhof.com/2023/javascript/a-small-javascript-pattern-i-enjoy-using/
- ✍️ What is a Factory Function?
- Lean Web Club • Learn a simpler way to build for the web with HTML, CSS, and vanilla JS. Get instant access to a growing collection of tutorials, projects, structured learning paths, and a supportive developer community.
- Why I’ve stopped exporting defaults from my JavaScript modules - Human Who Codes • After years of fighting with default exports, I’ve changed my ways.
- “Tip: The Intl.RelativeTimeFormat API gives us localized formatting of relative times (e.g. “yesterday”, “3 seconds ago”, “in 2 weeks”)… • Addi Osmani 💬
- Intl.RelativeTimeFormat • The Intl.RelativeTimeFormat object is a constructor for objects that enable language-sensitive relative time formatting (e.g., “3 hours ago”) • V8 JavaScript Engine
- String replacement in console.log • StackOverflow 💬