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

Getting a formatted date

7m:57s

If I ask you what’s the date today, I don’t expect a full-blown reply that says Tue Jan 23 2019 17:21:13 GMT+0800 (Singapore Standard Time). I’ll expect something along the lines of 23 Jan, 2019.

How do you get 23 Jan, 2019 from the Date object?

Built-in formats

Date comes with seven methods that lets you convert a Date object into a string. Here’s what each of them do:

  1. toString returns: Wed Jan 23 2019 17:23:42 GMT+0800 (Singapore Standard Time)
  2. toDateString returns: Wed Jan 23 2019
  3. toLocaleString returns: 23/01/2019, 17:23:42
  4. toLocaleDateString returns: 23/01/2019
  5. toGMTString returns: Wed, 23 Jan 2019 09:23:42 GMT
  6. toUTCString returns: Wed, 23 Jan 2019 09:23:42 GMT
  7. toISOString returns:2019-01-23T09:23:42.079Z

Each method gives a specific date/time string. Unfortunately, none of them gives us the format (23 Jan, 2019) we wanted.

We need to create the format ourselves with some Date methods.

Date methods

Here’s a list of methods that helps you format a date.

  1. getFullYear: Gets 4-digit year according to local time.
  2. getMonth: Gets month of the year (0-11) according to local time. Month is zero-indexed (which means January === 0 and December === 11).
  3. getDate: Gets day of the month (1-31) according to local time.
  4. getDay: Gets day of the week (0-6) according to local time. Day of the week begins with Sunday (0) and ends with Saturday (6).

So, what should you write to get 23 January, 2019?

Let’s work it through.

Creating a custom format

First, we need to create a date object.

const today = new Date()

From this point onwards, we’re going to assume the today is 23 January 2019. Substitute the values for today with the actual date when you work through this lesson.

Getting the day of the month (23) is simple. We can use getDate.

const day = today.getDate()
console.log(day) // 23

Getting the full year (2019) is simple too. We can use getFullYear.

const year = today.getFullYear()
console.log(year) // 2019

Getting January is harder.

We only have access to getMonth, and getMonth returns a number from 0 to 11. This means we must convert the month number (0-11) into a month string (January-December) ourselves.

First, we need to create an object that tells us 0 is January, 1 is February, 2 is March… and 11 is December.

const monthsInAYear = {
0: 'January',
1: 'February',
2: 'March',
3: 'April',
4: 'May',
5: 'June',
6: 'July',
7: 'August',
8: 'September',
9: 'October',
10: 'November',
11: 'December',
}

Notice this is a zero-indexed object? This object has the same structure as an Array. We can use an Array instead.

const monthsInAYear = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
]

To get January, we use getMonth to get monthNumber from the Date object. Then, we pass monthNumber into monthsInAYear to get January.

const monthIndex = today.getMonth()
const monthName = monthsInAYear[monthIndex]
console.log(monthName) // January

The condensed version:

const month = monthsInAYear[today.getMonth()]
console.log(month) // January

To get 23 January, 2019, we put the values we retrieved together in a template literal:

const dateString = `${day} ${month}, ${year}`
console.log(dateString) // 23 January, 2019

What if you want Jan instead of January?

If you want Jan (shortname) instead of January (longname), you have to modify monthsInAYear to include both shortnames and longnames.

const monthsInAYear = [
{ shortname: 'Jan', longname: 'January' },
{ shortname: 'Feb', longname: 'February' },
{ shortname: 'Mar', longname: 'March' },
{ shortname: 'Apr', longname: 'April' },
{ shortname: 'May', longname: 'May' },
{ shortname: 'Jun', longname: 'June' },
{ shortname: 'Jul', longname: 'July' },
{ shortname: 'Aug', longname: 'August' },
{ shortname: 'Sep', longname: 'September' },
{ shortname: 'Oct', longname: 'October' },
{ shortname: 'Nov', longname: 'November' },
{ shortname: 'Dec', longname: 'December' },
]

With the new monthsInAYear array, you get January with this:

const longMonth = monthsInAYear[today.getMonth()].longname
console.log(longMonth) // January

And Jan this way:

const shortMonth = monthsInAYear[today.getMonth()].shortname
console.log(shortMonth) // Jan

Getting day of the week

What if you wanted Wednesday, 23 January instead?

The Date object has a getDay method that tells us the day of the week. It returns a number from 0 to 6. The values goes like this:

  1. 0: Sunday
  2. 1: Monday
  3. 2: Tuesday
  4. 3: Wednesday
  5. 4: Thursday
  6. 5: Friday
  7. 6: Saturday

If we want to get Wednesday from Date we need to create a daysInAWeek array that converts numbers into strings.

const daysInAWeek = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
]

To get Wednesday we use the following:

const dayString = daysInAWeek[today.getDay()]
console.log(dayString) // Wednesday

If you want Wed you know what to do:

const daysInAWeek = [
{ shortname: 'Sun', longname: 'Sunday' },
{ shortname: 'Mon', longname: 'Monday' },
{ shortname: 'Tue', longname: 'Tuesday' },
{ shortname: 'Wed', longname: 'Wednesday' },
{ shortname: 'Thu', longname: 'Thursday' },
{ shortname: 'Fri', longname: 'Friday' },
{ shortname: 'Sat', longname: 'Saturday' },
]
const dayShortString = daysInAWeek[today.getDay()].shortname // Wed
const dayLongString = daysInAWeek[today.getDay()].longname // Wednesday

Exercise

Assuming today is 23 January 2019, get the following date strings:

  1. 23/01/2019
  2. 01/23/2019
  3. 23 Jan, 2019
  4. 23 Jan, Wed
  5. Wednesday, 23 January, 2019
  6. 23rd January, 2019

Answers

Answer for 23/01/2019:

const today = new Date()
const dateString = `${today.getDate()}/${
today.getMonth() + 1
}/${today.getFullYear()}`

Answer for 01/23/2019:

const today = new Date()
const dateString = `${
today.getMonth() + 1
}/${today.getDate()}/${today.getFullYear()}`

Answer for 23 Jan, 2019:

const monthsInAYear = [
{ shortname: 'Jan', longname: 'January' },
{ shortname: 'Feb', longname: 'February' },
{ shortname: 'Mar', longname: 'March' },
{ shortname: 'Apr', longname: 'April' },
{ shortname: 'May', longname: 'May' },
{ shortname: 'Jun', longname: 'June' },
{ shortname: 'Jul', longname: 'July' },
{ shortname: 'Aug', longname: 'August' },
{ shortname: 'Sep', longname: 'September' },
{ shortname: 'Oct', longname: 'October' },
{ shortname: 'Nov', longname: 'November' },
{ shortname: 'Dec', longname: 'December' },
]
const today = new Date()
const day = today.getDate()
const month = monthsInAYear[today.getMonth()].shortname
const year = today.getFullYear()
const dateString = `${day} ${month}, ${year}`

Answer for 23 Jan, Wed:

const monthsInAYear = [
{ shortname: 'Jan', longname: 'January' },
{ shortname: 'Feb', longname: 'February' },
{ shortname: 'Mar', longname: 'March' },
{ shortname: 'Apr', longname: 'April' },
{ shortname: 'May', longname: 'May' },
{ shortname: 'Jun', longname: 'June' },
{ shortname: 'Jul', longname: 'July' },
{ shortname: 'Aug', longname: 'August' },
{ shortname: 'Sep', longname: 'September' },
{ shortname: 'Oct', longname: 'October' },
{ shortname: 'Nov', longname: 'November' },
{ shortname: 'Dec', longname: 'December' },
]
const daysInAWeek = [
{ shortname: 'Sun', longname: 'Sunday' },
{ shortname: 'Mon', longname: 'Monday' },
{ shortname: 'Tue', longname: 'Tuesday' },
{ shortname: 'Wed', longname: 'Wednesday' },
{ shortname: 'Thu', longname: 'Thursday' },
{ shortname: 'Fri', longname: 'Friday' },
{ shortname: 'Sat', longname: 'Saturday' },
]
const today = new Date()
const day = today.getDate()
const month = monthsInAYear[today.getMonth()].shortname
const dayString = daysInAWeek[today.getDay()].shortname
const dateString = `${day} ${month}, ${dayString}`

Answer for Wednesday, 23 January, 2019:

const monthsInAYear = [
{ shortname: 'Jan', longname: 'January' },
{ shortname: 'Feb', longname: 'February' },
{ shortname: 'Mar', longname: 'March' },
{ shortname: 'Apr', longname: 'April' },
{ shortname: 'May', longname: 'May' },
{ shortname: 'Jun', longname: 'June' },
{ shortname: 'Jul', longname: 'July' },
{ shortname: 'Aug', longname: 'August' },
{ shortname: 'Sep', longname: 'September' },
{ shortname: 'Oct', longname: 'October' },
{ shortname: 'Nov', longname: 'November' },
{ shortname: 'Dec', longname: 'December' },
]
const daysInAWeek = [
{ shortname: 'Sun', longname: 'Sunday' },
{ shortname: 'Mon', longname: 'Monday' },
{ shortname: 'Tue', longname: 'Tuesday' },
{ shortname: 'Wed', longname: 'Wednesday' },
{ shortname: 'Thu', longname: 'Thursday' },
{ shortname: 'Fri', longname: 'Friday' },
{ shortname: 'Sat', longname: 'Saturday' },
]
const today = new Date()
const day = today.getDate()
const month = monthsInAYear[today.getMonth()].longname
const dayString = daysInAWeek[today.getDay()].longname
const year = today.getFullYear()
const dateString = `${dayString}, ${day} ${month}, ${year}`

Answer for 23rd January, 2019:

This one is trickier. You need to create the day suffix for rd too. Here’s the code:

const monthsInAYear = [
{ shortname: 'Jan', longname: 'January' },
{ shortname: 'Feb', longname: 'February' },
{ shortname: 'Mar', longname: 'March' },
{ shortname: 'Apr', longname: 'April' },
{ shortname: 'May', longname: 'May' },
{ shortname: 'Jun', longname: 'June' },
{ shortname: 'Jul', longname: 'July' },
{ shortname: 'Aug', longname: 'August' },
{ shortname: 'Sep', longname: 'September' },
{ shortname: 'Oct', longname: 'October' },
{ shortname: 'Nov', longname: 'November' },
{ shortname: 'Dec', longname: 'December' },
]
const dateSuffix = {
1: 'st',
2: 'nd',
3: 'rd',
4: 'th',
5: 'th',
6: 'th',
7: 'th',
8: 'th',
9: 'th',
10: 'th',
11: 'th',
12: 'th',
13: 'th',
14: 'th',
15: 'th',
16: 'th',
17: 'th',
18: 'th',
19: 'th',
20: 'th',
21: 'st',
22: 'nd',
23: 'rd',
24: 'th',
25: 'th',
26: 'th',
27: 'th',
28: 'th',
29: 'th',
30: 'th',
31: `st`,
}
const today = new Date()
const day = today.getDate()
const daySuffix = dateSuffix[day]
const month = monthsInAYear[today.getMonth()].longname
const year = today.getFullYear()
const dateString = `${day}${daySuffix} ${month}, ${year}`

Why do we use an object for dateSuffix? Simple, well, you can use an array too. But can you keep track of the correct number of ths without the numeric key?

Use what makes sense! :)