Syntax

Object Property Accessors

const person1 = {};
person1['firstname'] = 'Mario';
person1['lastname'] = 'Rossi';

console.log(person1.firstname);
// expected output: "Mario"

const person2 = {
  firstname: 'John',
  lastname: 'Doe'
};

console.log(person2['lastname']);
// expected output: "Doe"

Iteration

You can use the for ... of statement to iterate over an iterable object.

By default, if your iterator variable's mutability is not assigned with the const/let keywords, it is assumed to be a mutable variable.

String Manipulation

The substring() method returns the part of the string between the start and end indexes, or to the end of the string. Following is a simple example demonstrating usage of substring() to get last n characters from the string.

Date & Time

moment = new Date('2020-01-01')
// ISO 8601 format
moment = new Date('2020-01-01T04:05:06')

// RFC 3339 format
moment = new Date('2020-01-01 04:05:06')

// (year, month, day, hour, minute, seconds)
moment = new Date(2020, 1, 1, 4, 5, 6)

// UNIX Epoch
moment = new Date(1580544000000)
moment = new Date('2020-01-01')
formatter = new Intl.DateTimeFormat('en-US')
output = formatter.format(moment)
console.log(output)

Output

1/1/20

Modifying the Document

Event Listeners

An important concept in JavaScript is the ability to listen for and handle events. When we register an event listener, we give the browser an instruction to wait for a particular action to occur in the browser, and the the event handler contains the actual lines of code that will run when the event fires

(Aside: Web events are not part of the core JavaScript language, they are defined as part of the APIs built into the browser.)

The document readyState property

There are three values for document.readyState

When the value of this property changes, a readystatechange event fires on the document object.

The document DOMContentLoaded event

The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.

The window load event

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images. This is in contrast to DOMContentLoaded, which is fired as soon as the page DOM has been loaded, without waiting for resources to finish loading.

The <script> tag's defer attribute

Scripts with the defer attribute will prevent the DOMContentLoaded event from firing until the script has loaded and finished evaluating.

It's also worth noting that scripts with the defer attribute will execute in the order in which they appear in the document.

Forms

form.addEventListener('submit', (event) => {
  let data = event.target.elements
  contacts.push({
    firstName: data['fname'].value,
    lastName: data['lname'].value,
    email: data['email'].value
  })
  event.preventDefault()
  return false
})

The URL API

The Fetch API

There's a great example posted on Mozilla MDN, I'd consider checking it out.

WebKit

Objects and Properties

I was reading Mozilla's article "Objects and properties" and wanted to take notes on the content I found interesting.

To create an object, use an object initializer, a list of properties, separated by commas, enclosed in curly braces

Events

One of the hot methodologies in the JavaScript world is event delegation, and for good reason. Event delegation allows you to avoid adding event listeners to specific nodes; instead, the event listener is added to one parent. That event listener analyzes bubbled events to find a match on child elements. The base concept is fairly simple but many people don't understand just how event delegation works. Let me explain the how event delegation works and provide pure JavaScript example of basic event delegation.

Let's also say that something needs to happen when the user clicks each child element. You could add a separate event listener to each individual <li> element, but what if <li> elements are frequently added and removed from the list? Adding and removing event listeners would be a nightmare, especially if addition and removal code is in different places within your app. The better solution is to add an event listener to the parent <ul> element. But if you add the event listener to the parent, how will you know which element was clicked?

Simple: when the event bubbles up to the <ul> element, you check the event object's target property to gain a reference to the actual clicked node. Here's a very basic JavaScript snippet which illustrates event delegation:Start by adding a click event listener to the parent element. When the event listener is triggered, check the event element to ensure it's the type of element to react to. If it is an <li> element, boom: we have what we need! If it's not an element that we want, the event can be ignored. This example is pretty simple -- <ul> and <li> is a straight-forward comparison. Let's try something more difficult. Let's have a parent <div> with many children but all we care about is an A tag with the classA CSS class:

Start by adding a click event listener to the parent element. When the event listener is triggered, check the event element to ensure it's the type of element to react to. If it is an <li> element, boom: we have what we need! If it's not an element that we want, the event can be ignored. This example is pretty simple -- <ul> and <li> is a straight-forward comparison. Let's try something more difficult. Let's have a parent <div> with many children but all we care about is an A tag with the classA CSS class:

Using the Element.matches API, we can see if the element matches our desired target.

Triggering Events

The Clipboard API

The use of the clipboard requires permission, which you can get using the Permissions API.

Reading

Use the Clipboard object's readText() and read() methods.

Writing


TypeScript

The best open source resource to learn TypeScript is TypeScript Deep Dive

To compile TypeScript into JavaScript, use tsm, a command you can install from NPM

npm install --global typescript
mkdir project
cd project

npm init

npm install --save-dev typescript

tsc --init

The last command creates tsconfig.json, the TypeScript configuration file