Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JavaScript String at()

ES2022 introduced the at() method for strings.

Examples

Retrieve the third character from the name string.

const name = “W3Schools”;
let letter = name.at(2);

Access the third letter of the name string.

const name = “W3Schools”;
let letter = name[2];

The at() method returns an element at a specified index from a string.

It behaves the same as using traditional array indexing ([]).

The at() method has been supported in all modern browsers since March 2022.

js 14

RegExp d Modifier

ES2022 introduced the /d modifier to indicate the start and end of a match.

Example

let text = “aaaabb”;
let result = text.match(/(aa)(bb)/d);

RegExp modifiers are used to specify options such as case-insensitivity and to enable other types of global searches.

Modifier

Description

i

Perform case-insensitive matching

g

Perform a global match (find all)

m

Perform multiline matching

d

Perform substring matches (New in ES2022)

Object hasOwn

ES2022 introduces a safer way to check if a property is an own property of an object.

Object.hasOwn() works similarly to Object.prototype.hasOwnProperty, but it supports all object types.

Example

Object.hasOwn(“initProp”)

Error Cause

ES2022 allows you to specify the cause of an error using the error.cause property.

Example

try { connectData(); catch (err) { throw new Error(“Connecting failed.”, { cause: err }); }

JavaScript await import

JavaScript modules can now delay execution until resources that require import are loaded.

import {myData} from ‘./myData.js’;

const data = await myData();

JavaScript Class Field Declarations

class Hello {
  counter = 0// Class field
}
const myClass = new Hello();

let x = myClass.counter;

Class field declarations have been supported in all modern browsers since April 2021.

js 15

JavaScript Private Methods and Fields

class Hello {
  #counter = 0;  // Private field
  #myMethod() {} // Private method
}
const myClass = new Hello();

let x = myClass.#counter// Error
myClass.#myMethod();      // Error

Private methods and fields have been supported in all modern browsers since June 2021.

js 16