[フレーム]

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Dates JS Arrays JS Typed Arrays JS Sets JS Maps JS Math JS RegExp JS Data Types JS Errors JS Events JS Programming JS References JS UTF-8 Characters JS Versions

JS Advanced

JS Functions JS Objects JS Classes JS Iterations JS Asynchronous JS Modules JS HTML DOM JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


ECMAScript 2020


New Features in JavaScript 2020

FeatureDescription
BigInt Stores values too big to store in a JavaScript number
String matchAll() Searchs for all occurrences of a string in a string
Promise.allSettled() Takes promises as input and returns a single promise
Dynamic Import Loads JavaScript modules at runtime
globalThis Refers to the global object independent of code environment
import.meta Returns an object indicating the base URL of a module
Namespace Exports Export a full namespace from a module without having to import it first

New JavaScript Operators in 2020

OperDescription
?? Nullish coalescing returns the first argument not nullish
?. Optional chaining returns undefined if an object is undefined or null
&&= Logical AND assignment assigns the second value if the first value is true
||= Logical OR assignment assigns the second value if the first value is false
??= Nullish coalescing assignment assigns the second value if the first value is undefined or null

Browser Support

ECMAScript 2020 is supported in all modern browsers since April 2021.

Chrome
80
Edge
80
Firefox
80
Safari
14.1
Opera
67
Feb 2020 Feb 2020 Aug 2020 Apr 2021 Mar 2020

JavaScript BigInt

JavaScript BigInt variables are used to store big integer values that are too big to be represented by a a normal JavaScript Number.

JavaScript integers are only accurate up to about 15 digits.

Integer Example

let x = 999999999999999;
let y = 9999999999999999; // too big
Try it Yourself »

BigInt Example

let x = 9999999999999999;
let y = 9999999999999999n;
Try it Yourself »

To create a BigInt, append n to the end of an integer or call BigInt():

Example

let x = 1234567890123456789012345n;
let y = BigInt(1234567890123456789012345)
Try it Yourself »

The JavaScript typeof a BigInt is "bigint":

Example

let x = BigInt(999999999999999);
let type = typeof x;
Try it Yourself »

JavaScript String matchAll()

Before ES2020 there was no string method that could be used to search for all occurrences of a string in a string.

Example

const iterator = text.matchAll("Cats");
Try it Yourself »

If the parameter is a regular expression, the global flag (g) must be set set, otherwise a TypeError is thrown.

Example

const iterator = text.matchAll(/Cats/g);
Try it Yourself »

If you want to search case insensitive, the insensitive flag (i) must be set:

Example

const iterator = text.matchAll(/Cats/gi);
Try it Yourself »

Note

ES2021 introduced the string method replaceAll().



The Nullish Coalescing Operator (??)

The ?? operator returns the first operand if it is not nullish. Otherwise it returns the second.

The ?? operator returns the second operand if the first operand is nullish.

Nullish is a word for describing null or undefined.

The ?? operator is used to provide default values and is distinct from the logical OR operator || because it only treats null and undefined as "nullish" and ignores other "falsy" values like 0 or an empty string.

Example

let name = null;
let text = "missing";
let result = name ?? text;
Try it Yourself »

The Optional Chaining Operator (?.)

The Optional Chaining Operator returns undefined if an object property is undefined or null (instead of throwing an error).

Example

const car = {type:"Fiat", model:"500", color:"white"};
let name = car?.name;
Try it Yourself »

Optional chaining not only works on object properties, but also on function calls and arrays.

Function Calls

Use CaseSyntaxBehavior
Safe function callobj.method?.() Returns undefined if method does not exist
Return handlinglet x = fn?.()x is undefined if fn does not exist

Arrays

Use CaseSyntaxReturns
Safe element accessarr?.[2] undefined if arr is null or undefined
Safe property on elementarr?.[1]?.name Returns undefined safely
Safe method callarr?.map(fn) Returns undefined if arr does not exist
Default value fallbackarr?.[0] ?? "N/A" Returns "N/A" if arr or element is missing

The &&= Operator

The Logical AND Assignment Operator is used between two values.

If the first value is true, the second value is assigned.

Logical AND Assignment Example

let x = 10;
x &&= 5;
Try it Yourself »

The ||= Operator

The Logical OR Assignment Operator is used between two values.

If the first value is false, the second value is assigned.

Logical OR Assignment Example

let x = 10;
x ||= 5;
Try it Yourself »

The ??= Operator

The Nullish Coalescing Assignment Operator is used between two values.

If the first value is undefined or null, the second value is assigned.

Nullish Coalescing Assignment Example

let x;
x ??= 10;
Try it Yourself »

JavaScript Promise.allSettled()

The Promise.allSettled() method returns a single Promise from a list of promises.

Example

// Create a Promise
const myPromise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 200, "King");
});

// Create another Promise
const myPromise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, "Queen");
});

// Settle All
Promise.allSettled([myPromise1, myPromise2]).then((results) =>
results.forEach((x) => myDisplay(x.status)),
);
Try it Yourself »

Note

Promise.allSettled() means "Just run all promises. I don't care about the results".


JavaScript Dynamic Import

Dynamic Import is a way to load JavaScript modules at runtime, rather than at the start of your program.

Syntax

import("./module.js")
  • The argument must be a string or expression that resolves to a path
  • You must run the import inside a module script (<script type="module">)

Unlike static imports (which must appear at the top of a file), dynamic imports can be used anywhere - inside functions, conditionals, event handlers, etc.

TypeExampleWhen Loaded
Staticimport { add } from './math.js'; At load time
Dynamicconst math = await import('./math.js'); When needed

JavaScript Global This

globalThis provides a standard way to access the global object, regardless of the environment your the code runs in: In the browser, Node.js, in a Web Worker, or another JS runtime.

The global object is the top-level object in a JavaScript environment:

  • In browsers, it is window.
  • In Node.js, it is global.
  • In Web Workers, it is self.

Many environments have their own name for the this object. This caused compatibility issues for code meant to run everywhere.

globalThis was introduced in ECMAScript 2020 (ES11) to solve that problem. It is a unified reference to the global object, no matter the environment.

Examples

globalThis === window); // true in browsers
globalThis === global); // true in Node.js
Try it Yourself »

Example

Defining a truly global variable:

globalThis.appName = "W3Schools Demo";
Try it Yourself »

Previously, developers had to use workarounds like:

Example

const getGlobal = function() {
if (typeof self !== 'undefined') return self;
if (typeof window !== 'undefined') return window;
if (typeof global !== 'undefined') return global;
throw new Error('Cannot find global object');
};

With globalThis, that is no longer needed.


JavaScript import.meta

import.meta returns an object containing the base URL of a module.

Example

const myObject = import.meta;
Try it Yourself »

Module Namespace Exports

With module namespace exports, you can re-export an entire namespace from your own module, without having to import it first.

Example

Before:

// re-export all named exports directly
export * from "./utils.js";
// consumers can use:
import { add } from "./math.js";

Now:

// re-export all as a namespace
export * as utils from "./utils.js";
// consumers can use:
import { utils } from "./math.js";

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

AltStyle によって変換されたページ (->オリジナル) /