Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS 2019

JavaScript Version Numbers

Earlier ECMAScript versions were named by numbers, such as ES5 and ES6. Starting in 2016, versions are named based on the release year, like ES2016, ES2018, ES2020, and so on.

New Features in ES2019

  • String.trimStart()
  • String.trimEnd()
  • Object.fromEntries
  • Optional catch binding
  • Array.flat()
  • Array.flatMap()
  • Revised Array.Sort()
  • Revised JSON.stringify()
  • Separator symbols allowed in string litterals
  • Revised Function.toString()

JavaScript String trimStart()

ES2019 introduced the trimStart() method in JavaScript. It works similarly to trim(), but removes whitespace only from the beginning of a string.

Example

let text1 = ”     Hello World!     “;
let text2 = text1.trimStart();

The JavaScript String.trimStart() method has been supported in all modern browsers since January 2020.

js 19

JavaScript Object fromEntries()

ES2019 introduced the Object.fromEntries() method in JavaScript. This method creates an object from iterable key-value pairs.

Example

const fruits = [
[“apples”300],
[“pears”900],
[“bananas”500]
];

const myObj = Object.fromEntries(fruits);

The JavaScript Object.fromEntries() method has been supported in all modern browsers since January 2020.

js 20

Optional catch Binding

Starting with ES2019, you can omit the catch parameter if it is not needed.

Example

Before 2019:

try {
// code
catch (err) {
// code
}

After 2019:

try {
// code
catch {
// code
}

Optional catch binding has been supported in all modern browsers since January 2020.js 1

JavaScript Array flat()

ES2019 introduced the Array.flat() method in JavaScript. This method creates a new array by flattening a nested array.

Example

const myArr = [[1,2],[3,4],[5,6]];
const newArr = myArr.flat();

The JavaScript Array.flat() method has been supported in all modern browsers since January 2020.

js 2

JavaScript Array flatMap()

ES2019 introduced the Array.flatMap() method in JavaScript.

This method first maps all elements of an array, then creates a new array by flattening the result.

Example

const myArr = [123456];
const newArr = myArr.flatMap(x => [x, x * 10]);

Stable Array sort()

ES2019 revised the Array.sort() method to require a stable sorting algorithm.

Prior to ES2019, the specification allowed unstable algorithms like QuickSort. After ES2019, browsers must use a stable sorting algorithm, ensuring that elements with the same value maintain their relative positions.

Example

const myArr = [
  {name:“X00”,price:100 },
  {name:“X01”,price:100 },
  {name:“X02”,price:100 },
  {name:“X03”,price:100 },
  {name:“X04”,price:110 },
  {name:“X05”,price:110 },
  {name:“X06”,price:110 },
  {name:“X07”,price:110 }
];

In the example above, when sorting by price, the names should retain their original relative positions if their prices are the same, ensuring a stable sort.

X01 100
X03 100
X00 100
X03 100
X05 110
X04 110
X06 110
X07 110

Revised JSON.stringify()

ES2019 revised the JSON.stringify() method to allow characters encoded with a backslash (\) to be properly stringified, which was not possible before 2019.

Example

let text = JSON.stringify(“\u26D4”);

Before ES2019, using JSON.stringify() on UTF-8 code points (U+D800 to U+DFFF) would result in broken Unicode characters, like ���.

With the ES2019 revision, strings containing these UTF-8 code points are now safely converted with JSON.stringify() and can be accurately restored using JSON.parse().

Separator Symbols

Before 2019, line separator and paragraph separator symbols (\u2028 and \u2029) in string literals were treated as line terminators, causing errors. After the revision, these characters are now allowed in string literals without causing exceptions.

Example

// This is valid in ES2019:
let text = “\u2028”;

Note

Before ES2019:

JSON.parse(‘”\u2028″‘) would return an empty string (”).

‘”\\u2028″‘ would result in a syntax error.

After the revision, JavaScript and JSON follow the same rules, allowing these characters to be parsed and used correctly.

Revised Function toString()

ES2019 revised the Function.toString() method. Prior to 2019, different browsers returned variations of a function’s source code, often omitting comments, spaces, or formatting.

From 2019 onwards, toString() must return the function’s source code exactly as written, including comments, spaces, and all syntax details.

Example

function myFunction(p1, p2) {
  return p1 * p2;
}