Modern Syntax
Modern syntax for creating strings and safely accessing values.
| Term / Syntax | Name | Description | Example |
|---|---|---|---|
template literal |
Interpolated String | Creates strings with embedded expressions. | const message = `Hello, ${name}!`; |
?. |
Optional Chaining | Safely accesses a property by returning undefined if the preceding value is null or undefined. |
user.profile?.avatar |
Classes
Creating object templates, instances, and inheritance relationships with classes.
| Term / Syntax | Name | Description | Example |
|---|---|---|---|
class |
Class Declaration | Defines a template for creating objects with related properties and methods. | class User { constructor(name) { this.name = name; } } |
constructor() |
Instance Initialization | Initializes a new object when an instance of a class is created. | constructor(name) { this.name = name; } |
new |
Create Instance | Creates a new object instance by calling a constructor or class. | const user = new User("Jennifer"); |
extends |
Class Inheritance | Creates a class that inherits properties and methods from another class. | class Admin extends User { } |
super |
Parent Class Access | Calls the parent class constructor or accesses its methods. | constructor(name) { super(name); } |
Promises & Async
Working with asynchronous results, Promise handlers, and async and await syntax.
| Term / Syntax | Name | Description | Example |
|---|---|---|---|
Promise |
Asynchronous Result | Represents the eventual completion or failure of an asynchronous operation. | const promise = fetch(url); |
.then() |
Promise Fulfillment Handler | Runs a function when a Promise is fulfilled and returns a new Promise. | promise.then(value => console.log(value)); |
.catch() |
Promise Rejection Handler | Runs a function when a Promise is rejected and returns a new Promise. | promise.catch(error => console.error(error)); |
.finally() |
Promise Completion Handler | Runs a function when a Promise settles, whether it is fulfilled or rejected. | promise.finally(() => hideLoading()); |
Promise.all() |
Wait for Multiple Promises | Waits for all provided Promises to fulfill and returns their values together, but rejects if any Promise rejects. | const results = await Promise.all([first, second]); |
Promise.allSettled() |
Wait for All Promise Results | Waits for all provided Promises to settle and returns the outcome of each one. | const results = await Promise.allSettled([first, second]); |
async |
Asynchronous Function | Declares a function that always returns a Promise and allows await inside it. |
async function loadData() { } |
await |
Wait for Promise | Waits for a Promise to settle, returning its fulfilled value or throwing if it rejects. | const data = await loadData(); |
Modules
Sharing and loading values between JavaScript modules.
| Term / Syntax | Name | Description | Example |
|---|---|---|---|
export |
Module Export | Makes values available to another module. | export function formatDate(date) { } |
import |
Module Import | Loads exported values from another module. | import { formatDate } from "./utils.js"; |
Error Handling
Creating, throwing, and handling errors when JavaScript operations fail.
| Term / Syntax | Name | Description | Example |
|---|---|---|---|
new Error() |
Error Object | Creates an Error object with a message describing what went wrong. | const error = new Error("Invalid value"); |
try...catch |
Error Handling | Runs code that may throw an error and handles the error if one occurs. | try { parseData(); } catch (error) { console.error(error); } |
throw |
Throw Error | Throws an error that can be handled by surrounding error-handling code. | throw new Error("Invalid value"); |