Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Learn JavaScript
Lesson Plan
  1. JavaScript and its ecosystem
    1. Welcome to Learn JavaScript!
    2. What is JavaScript used for?
    3. The JavaScript ecosystem
    4. Varying versions of JavaScript
  2. JS Basics
    1. Linking your JavaScript file
    2. Preparing your text editor
    3. The Console
    4. Comments
    5. On semicolons
    6. Strings, numbers and booleans
    7. Understanding Variables
    8. Understanding Functions
    9. The Flow of a Function
    10. Arrow functions
    11. Intro to objects
    12. If/else statements
    13. Comparing Objects
    14. The NOT operator
    15. Null and Undefined
    16. The BOM and the DOM
    17. Selecting an Element
    18. Changing Classes
    19. Listening to events
    20. Callbacks
  3. Building simple components
    1. How to think like a developer
    2. Starter files and Source codes
    3. Do this for every component
    4. πŸ›  Off-canvas menu: Building an off-canvas menu
    5. πŸ›  Modal: Building a Modal
    6. Lessons from the building process
    7. Debugging errors
    8. How to use a linter
  4. Arrays and loops
    1. Introduction to Arrays
    2. Array methods
    3. For loops
    4. The forEach loop
    5. Selecting multiple elements
    6. Nodes vs Elements
    7. πŸ›  Accordion: Building an accordion
  5. Dom basics
    1. Id, classes, attributes, and tags
    2. Changing CSS with JavaScript
    3. Getting CSS with JavaScript
    4. Changing Attributes
    5. Finding an element's size and position
    6. DOM Traversals
    7. πŸ›  Tabby: Building Tabby (A Tabbed component)
    8. πŸ›  Carousel: HTML and CSS
    9. πŸ›  Carousel: Switching slides with JavaScript
    10. πŸ›  Carousel: Working the dots
    11. πŸ›  Carousel: Positioning slides with JavaScript
  6. Events deep dive
    1. The listening element
    2. Default Behaviors
    3. Event propagation
    4. Event delegation
    5. Removing Event Listeners
    6. πŸ›  Modal: Closing the modal
    7. πŸ›  Accordion: Event delegation
    8. πŸ›  Tabby: Event delegation
    9. πŸ›  Carousel: Event delegation
  7. Transitions and Animations
    1. CSS Transitions
    2. CSS Animations
    3. Silky-smooth animations
    4. Integrating CSS transitions and animations with JavaScript
    5. Animating with JavaScript
    6. GreenSock Animation API (GSAP)
    7. πŸ›  Off-canvas menu: Animations
    8. πŸ›  Modal: Animating the modal
    9. πŸ›  Modal: Animating the pointing hand
    10. πŸ›  Modal: Animating the waving hand
    11. πŸ›  Modal: Wave hand animation with JavaScript (using GSAP)
    12. πŸ›  Accordion: Animations
    13. πŸ›  Carousel: Animations
  8. Useful JS features
    1. Ternary operators
    2. AND and OR operators
    3. Early returns
    4. Template Literals
    5. Destructuring
    6. Default parameters
    7. Enhanced Object Literals
    8. Rest and Spread
    9. Useful array methods
    10. Looping through objects
    11. Returning objects with implicit return
    12. πŸ›  Accordion: Using useful JavaScript features
    13. πŸ›  Tabby: Using useful JavaScript features
    14. πŸ›  Carousel: Useful JavaScript features
  9. JS Best practices
    1. Write declarative code
    2. Functions with a purpose
    3. Manage scope
    4. Reduce state changes
    5. Don't reassign
    6. Don't mutate
    7. Preventing Objects from mutating
    8. Preventing Arrays from mutating
    9. Write pure functions
    10. πŸ›  Accordion: Refactor
    11. πŸ›  Carousel: First refactor
    12. πŸ›  Carousel: Refactoring the dots part
    13. πŸ›  Carousel: Previous and next buttons
    14. πŸ›  Carousel: Second refactor
  10. Manipulating text and content
    1. Changing Text and HTML
    2. Creating HTML Elements
    3. Adding multiple elements to the DOM
    4. Removing Elements from the DOM
    5. πŸ›  Carousel: Creating dots with JavaScript
    6. πŸ› οΈ Calculator: HTML and CSS
    7. πŸ› οΈ Calculator: Happy Path
    8. πŸ› οΈ Calculator: Testing the Happy Path
    9. πŸ› οΈ Calculator: Easy Edge Cases
    10. πŸ› οΈ Calculator: Difficult Edge Cases
    11. πŸ› οΈ Calculator: Refactoring
    12. The switch statement
    13. πŸ› οΈ Calculator: Refactoring (Part 2)
    14. πŸ› οΈ Popover: Making one popover
    15. πŸ› οΈ Popover: Making four popovers
    16. πŸ› οΈ Popover: Making popovers with JavaScript
  11. Handling Forms
    1. Intro to forms
    2. Selecting form fields with JavaScript
    3. Form fields and their events
    4. Sanitize your output
    5. Generating unique IDs
    6. πŸ› οΈ Popover: Dynamic ID
    7. πŸ› οΈ Todolist: The HTML and CSS
    8. πŸ› οΈ Todolist: Creating tasks with JavaScript
    9. πŸ› οΈ Todolist: Deleting tasks with JavaScript
    10. πŸ› οΈ Typeahead: The HTML and CSS
    11. πŸ› οΈ Typeahead: Displaying predictions
    12. πŸ› οΈ Typeahead: Selecting a prediction
    13. πŸ› οΈ Typeahead: Bolding search terms
  12. Handling Dates
    1. The Date object
    2. Getting a formatted date
    3. Getting the time
    4. Local time and UTC Time
    5. Setting a specific date
    6. Setting a date with Date methods
    7. Adding (or subtracting) date and time
    8. Comparing Dates and times
    9. πŸ› οΈ Datepicker: HTML and CSS
    10. πŸ› οΈ Datepicker: Building the calendar
    11. πŸ› οΈ Datepicker: Building the datepicker with JavaScript
    12. πŸ› οΈ Datepicker: Previous and Next buttons
    13. πŸ› οΈ Datepicker: Selecting a date
    14. πŸ› οΈ Datepicker: Positioning the datepicker
    15. πŸ› οΈ Datepicker: Showing and hiding
    16. Formatting a date with toLocaleString
    17. setTimeout
    18. setInterval
    19. πŸ› οΈ Countdown timer: HTML and CSS
    20. πŸ› οΈ Countdown timer: JavaScript
    21. πŸ› οΈ Countdown timer: Counting Months
    22. πŸ› οΈ Countdown timer: Daylight Saving Time
    23. πŸ› οΈ Countdown timer: Counting Years
  13. Async JS
    1. Introduction to Ajax
    2. Understanding JSON
    3. The Fetch API
    4. Possible data types
    5. JavaScript Promises
    6. Requests and responses
    7. Sending a POST request
    8. Authentication
    9. Handling errors
    10. Viewing response headers
    11. CORS and JSONP
    12. XHR vs Fetch
    13. Using an Ajax library
    14. Reading API documentation
    15. Understanding curl
    16. πŸ› οΈ Todolist: The Todolist API
    17. πŸ› οΈ Todolist: Fetching tasks
    18. πŸ› οΈ Todolist: Creating tasks
    19. πŸ› οΈ Todolist: Editing tasks
    20. πŸ› οΈ Todolist: Deleting tasks
    21. πŸ› οΈ Todolist: Creating tasks with Optimistic UI
    22. πŸ› οΈ Todolist: Handling Optimistic UI errors
    23. πŸ› οΈ Todolist: Editing tasks with Optimistic UI
    24. πŸ› οΈ Todolist: Deleting tasks with Optimistic UI
    25. πŸ› οΈ Todolist: Refactor
    26. πŸ› οΈ Typeahead: How to add Ajax
    27. πŸ› οΈ Typeahead: Adding Ajax
    28. πŸ› οΈ Typeahead: Handling errors
    29. πŸ› οΈ Google Maps Clone: Creating your first Google Map
    30. πŸ› οΈ Google Maps Clone: Fetching JSONP via JavaScript
    31. πŸ› οΈ Google Maps Clone: Drawing directions
    32. πŸ› οΈ Google Maps Clone: Driving directions
    33. πŸ› οΈ Google Maps Clone: Handling errors
    34. πŸ› οΈ Google Maps Clone: Adding stopovers
    35. πŸ› οΈ Google Maps Clone: Refactor
  14. Advanced Async JS
    1. Requesting many resources at once
    2. Asynchronous functions
    3. Handling multiple awaits
    4. Asynchronous loops
    5. πŸ› οΈ Dota Heroes: Listing heroes
    6. πŸ› οΈ Dota Heroes: Filtering heroes (Part 1)
    7. πŸ› οΈ Dota Heroes: Filtering heroes (Part 2)
    8. πŸ› οΈ Dota Heroes: Refactoring
    9. πŸ› οΈ Dota Heroes: Hero Page
    10. πŸ› οΈ Dota Heroes: Making the hero page robust
    11. πŸ› οΈ Dota Heroes: Heroes page refactor
  15. Handling Keyboard Events
    1. Keyboard users
    2. Handling commonly used keys
    3. Keyboard events
    4. Understanding Tabindex
    5. Detecting the focused element
    6. Directing focus
    7. Preventing people from tabbing into elements
    8. How to choose keyboard shortcuts
    9. Creating single-key shortcuts
    10. πŸ› οΈ Off-canvas: Adding keyboard interaction
    11. πŸ› οΈ Modal: Adding keyboard interaction
    12. πŸ› οΈ Accordion: Adding keyboard interaction
    13. πŸ› οΈ Tabby: Adding keyboard interaction
    14. πŸ› οΈ Tabby: Refactoring
    15. πŸ› οΈ Carousel: Adding keyboard interaction
    16. πŸ› οΈ Carousel: Displaying help text
    17. πŸ› οΈ Calculator: Adding keyboard interaction
    18. πŸ› οΈ Popover: Keyboard
    19. πŸ› οΈ Popover: Refactor
    20. Keyboard shortcuts with Command and Control modifiers
    21. πŸ› οΈ Todolist: Keyboard
    22. πŸ› οΈ Typeahead: Keyboard
    23. πŸ› οΈ Typeahead: Selecting a prediction with the keyboard
    24. πŸ› οΈ Google Maps Clone: Keyboard
    25. πŸ› οΈ Dota Heroes: Keyboard
    26. πŸ› οΈ Datepicker: Tabbing in and out
    27. πŸ› οΈ Datepicker: Keyboard shortcuts
  16. Screen reader accessibility
    1. What is accessibility?
    2. How to use a screen reader
    3. Using NVDA
    4. Using Voiceover
    5. Aria roles
    6. Landmark roles
    7. Document structure roles
    8. Live region roles
    9. Widget roles
    10. Window and Abstract roles
    11. Accessible names and descriptions
    12. Hiding content
    13. ARIA properties and ARIA states
    14. ARIA for expandable widgets
    15. πŸ› οΈ Off-canvas: Accessibility
    16. ARIA for modal dialogs
    17. πŸ› οΈ Modal: Screen reader accessibility
    18. πŸ› οΈ Accordion: Screen reader accessibility
    19. ARIA for Tabbed components
    20. πŸ› οΈ Tabby: Screen reader accessibility
    21. πŸ› οΈ Tabby: Refactor
    22. πŸ› οΈ Carousel: Screen reader accessibility
    23. Roles that trigger Forms and Application modes
    24. What's next for accessibility?
  17. Handling Scroll
    1. The Scroll event
    2. πŸ› οΈ Auto-hiding Sticky-nav: HTML and CSS
    3. πŸ› οΈ Auto-hiding Sticky-nav: JavaScript
    4. πŸ› οΈ Auto-hiding Sticky-nav: Natural reveal
    5. Intersection Observer API
    6. Intersection Observer Options
    7. πŸ› οΈ Slide & Reveal
    8. πŸ› οΈ Slide & Reveal: Always fade-in when you scroll down
    9. πŸ› οΈ Slide & Reveal: Fine-tuning the animation
    10. πŸ› οΈ Infinite Scroll: Anatomy
    11. πŸ› οΈ Infinite Scroll: Infinite load
    12. πŸ› οΈ Infinite Scroll: Refactor
    13. πŸ› οΈ Infinite Scroll: Implementing the Infinite Scroll
  18. Mouse, Touch, and Pointer events
    1. Mouse Events
    2. πŸ› οΈ Spinning Pacman: HTML and CSS
    3. πŸ› οΈ Spinning Pacman: JavaScript
    4. Touch events
    5. Pointer events
    6. Touch-action
    7. πŸ› οΈ Spinning Pacman: Supporting Touch
    8. Cloning elements
    9. πŸ› οΈ DragDrop: HTML and CSS
    10. πŸ› οΈ DragDrop: JavaScript
    11. πŸ› οΈ DragDrop: Creating a drop preview
    12. πŸ› οΈ DragDrop: Sortable drop preview
    13. πŸ› οΈ DragDrop: Robustness
    14. πŸ› οΈ DragDrop: Refactor
  19. Object Oriented Programming
    1. Before we begin
    2. What is Object Oriented Programming?
    3. Four Flavours of Object Oriented Programming
    4. Inheritance
    5. This in JavaScript
    6. Call, bind, apply
    7. Creating Derivative Objects
    8. Composition vs Inheritance
    9. Polymorphism
    10. Encapsulation
    11. Closures
    12. Encapsulation in Object Oriented Programming
    13. Getters and Setters
    14. What OOP flavour to use
    15. When to use Object Oriented Programming
  20. Writing reusable code
    1. Creating reusable code by writing libraries
    2. Two Types of libraries
    3. Including libraries with Script tags
    4. Including libraries with ES6 Modules
    5. Dynamic imports
    6. πŸ› οΈ Off Canvas: Building a Library
    7. πŸ› οΈ Modal: Library setup
    8. πŸ› οΈ Modal: Opening the Modal
    9. πŸ› οΈ Modal: Closing the modal
    10. πŸ› οΈ Modal: Inheritance and Polymorphism
    11. πŸ› οΈ Modal: Resolving differences between subclasses
    12. πŸ› οΈ Modal: Exposing properties and methods
    13. πŸ› οΈ Accordion: Building a library
    14. πŸ› οΈ Tabby: Building a library
    15. πŸ› οΈ Carousel: Building a library
    16. πŸ› οΈ Calculator: Library
    17. πŸ› οΈ Calculator: Fixing the Clear Key
    18. πŸ› οΈ Calculator: Handling other keys
    19. πŸ› οΈ Calculator: State
    20. πŸ› οΈ Popover: Library
    21. πŸ› οΈ Popover: Adding event listeners
    22. πŸ› οΈ Typeahead: Library
    23. πŸ› οΈ DatePicker: Library
  21. Going from Vanilla JS to JS Frameworks
    1. πŸ› οΈ Building a Tiny framework
    2. πŸ› οΈ Tiny: Add event listeners
    3. πŸ› οΈ Tiny: Updating state
    4. πŸ› οΈ Tiny: Rendering Child Components
    5. πŸ› οΈ Tiny: Changing Parent State
    6. πŸ› οΈ Tiny: Passing Props
    7. πŸ› οΈ Tiny: Multiple Props
    8. πŸ› οΈ Tiny: Passing values from sibling components
    9. πŸ› οΈ Tiny: Mounting
    10. πŸ› οΈ Tiny: Passing props to descendants
    11. πŸ› οΈ Tiny: A tiny refactor
  22. Single Page Apps
    1. What is a Single Page App?
    2. Simple SPA using only CSS
    3. The Location Interface
    4. The History Interface
    5. Minimum viable server for a SPA
    6. πŸ› οΈ Dota SPA: Introduction
    7. πŸ› οΈ Dota SPA: Building The Heroes List
    8. πŸ› οΈ Dota SPA: Building the filters
    9. πŸ› οΈ Dota SPA: Filtering heroes
    10. πŸ› οΈ Dota SPA: Displaying filtered heroes
    11. πŸ› οΈ Dota SPA: Getting Ready to build the Hero Page
    12. πŸ› οΈ Dota SPA: Building the hero page
    13. πŸ› οΈ Dota SPA: Lore and abilities
    14. πŸ› οΈ Dota SPA: Routing for Single-page apps

JavaScript Promises

4m:36s

β€œDo you promise to buy a black forest cake for my birthday next week?”, your daughter asks.

β€œYeah, I promise”, you say.

Promises in JavaScript are like promises in real lifeβ€”we don’t know if the promise will be fulfilled when it is made.

How a promise works

In JavaScript, a promise is an object that will return a value in future. The promise between you and your daughter will look somewhat like this:

const promise = buyCake('black forest')

We don’t know if you’ll fulfill the promise when you made it. At this point, we say the promise is pending.

Fulfilling a promise

You fulfill your promise to your daughter if you show up with a black forest birthday cake on her birthday. In JavaScript, we say the promise is fulfilled or resolved.

When a promise gets fulfilled, you carry out the next set of instructions in a then method. then takes in a callback that accepts one argument.

buyCake('black forest').then(celebrateBirthday)

Some people call promises thenables (then-able) because they have a then method.

Rejecting a promise

If you did not show up with a black forest birthday cake in hand, the promise is not fulfilled. In JavaScript, we say the promise is rejected.

When a promise gets rejected, you perform a contingency plan in a catch method.

buyCake('black forest').then(celebrateBirthday).catch(postponeCelebration)

Constructing a promise

You can construct a promise by writing new Promise. Promise here is a constructor (a function that creates other objects). Each constructed promise is a function takes in two valuesβ€”resolve and reject.

new Promise((resolve, reject) => {
/* Do something here */
})

resolve and reject are functions that take in one value each. If resolve gets called, the promise succeeds and goes into the next then method. If reject gets called, the promise fails and goes into the catch method.

This means you need a condition to determine if a promise resolves or rejects.

new Promise((resolve, reject) => {
if (condition) {
resolve('😁')
} else {
reject('😒')
}
})
// This version is written with ternary operators
new Promise((resolve, reject) => {
condition ? resolve('😁') : reject('😒')
})

In the birthday cake example above, you need to show up with a black forest birthday cake. This is the condition for the promise.

function buyCake(cakeType) {
return new Promise((resolve, reject) => {
cakeType === 'black forest' ? resolve('😁') : reject('😒')
})
}

The above code works, but Standard (the linter we use) will give you a red squiggly underline in the reject method. It argues that you should create an error when you reject a promise.

Why?

Because errors give you a list of functions that were called before the error occurred. This list of functions is a call stack.

To create an error message, you use new Error.

function buyCake(cakeType) {
return new Promise((resolve, reject) => {
cakeType === 'black forest' ? resolve('😁') : reject(new Error('😒'))
})
}

If you pass black forest into buyCake, the promise fulfills and the then callback gets called.

buyCake('black forest')
.then(reaction => console.log(reaction)) // 😁
.catch(reaction => console.log(reaction)) // Skipped

If you pass something else into buyCake, the promise rejects and the catch callback gets called.

buyCake('pikachu cake')
.then(reaction => console.log(reaction)) // Skipped
.catch(reaction => console.log(reaction)) // 😒

The buyCake promise we built together is a simple one; we resolve or reject the promise immediately without waiting for an actual response from a server.

An example of a real promise is an Ajax call with Fetch.

Fetch and promises

A Fetch request is a promise. You can see it if you log it into the console.

const promise = fetch('https://api.github.com/users/zellwk/repos')
console.log(promise)
A fetch request is a promise

Note: the pending status turns to resolved quite quickly. You have to be fast to catch it :)

Chained then calls

If you return a value in a then call, that value will be available in the next then call.

buyCake('black forest')
.then(reaction => reaction + reaction) // 😁
.then(reaction => console.log(reaction)) // 😁😁

This means the code we wrote in the previous lessons can be written as the following:

fetch('https://api.github.com/users/zellwk/repos')
.then(response => response.json())
.then(data =>
data.map(repo => {
return {
name: repo.name,
url: repo['html_url'],
stars: repo['stargazers_count'],
}
}),
)
.then(data =>
data
.map(
repo =>
`<li><a href="${repo.url}">${repo.name} (${repo.stars} stars)</a></li>`,
)
.join(''),
)
.then(HTMLString => {
const ol = document.createElement('ol')
ol.innerHTML = HTMLString
document.body.append(ol)
})

This looks complicated. But if we refactor it with named callbacks, the code becomes quite elegant.

fetch('https://api.github.com/users/zellwk/repos')
.then(response => response.json())
.then(massageData)
.then(convertDataToHTMLString)
.then(createList)

One catch to catch them all

If an error occurs on a promise chain, the error will be passed into the catch call. Any then methods that are not called yet will be skipped.

fetch('https://api.github.com/users/zellwk/repos')
.then(response => response.json())
.then(data => {
// An error occurs!
return new Error('Something went wrong!')
})
// JavaScript skips these 'then' methods
.then(massageData)
.then(convertDataToHTMLString)
.then(createList)
// JavaScript calls this catch method
.catch(e => console.log(e)) // Error: Something went wrong!

Finally

Promises also have a finally method. This method will be called after all then and catch calls.

buyCake('black forest')
.then(celebrateBirthday)
.catch(postponeCelebration)
.finally(goToBed)

Exercise

Answer these questions:

  1. What is a promise?
  2. What happens when a promise resolves?
  3. What happens when a promise rejects?
  4. Can you chain multiple then methods?
  5. When is finally called?