JavaScript 2019
JavaScript Basics

JavaScript 2019

ECMAScript 2019 Overview

Previously, ECMAScript versions were known by numerical designations such as ES5 and ES6. However, starting in 2016, versions are identified by the year of their release, such as ES2016, ES2018, and so forth. ECMAScript 2019 introduces several new features aimed at improving the functionality and efficiency of JavaScript.

New Features in ES2019:

  • String trimming methods: String.trimStart() and String.trimEnd()
  • Object enhancements: Object.fromEntries()
  • Syntax simplifications: Optional catch binding
  • Array improvements: Array.flat(), Array.flatMap(), and a stable Array.sort()
  • JSON enhancements: Revised JSON.stringify()
  • Syntax allowances: Separator symbols in string literals
  • Detailed function representation: Revised Function.toString()

Feature Details and Examples

1. String.trimStart() and String.trimEnd()

These methods provide a way to remove whitespace from the beginning (trimStart()) and end (trimEnd()) of a string.

let text = "     Hello World!     ";
let trimmedStart = text.trimStart(); // "Hello World!     "
let trimmedEnd = text.trimEnd();   // "     Hello World!"

Supported in all modern browsers as of January 2020:

  • Chrome 66, Edge 79, Firefox 61, Safari 12, Opera 50

2. Object.fromEntries()

This method transforms a list of key-value pairs into an object.

const entries = [['apples', 300], ['pears', 900], ['bananas', 500]];
const fruitObj = Object.fromEntries(entries); // { apples: 300, pears: 900, bananas: 500 }

Supported since January 2020:

  • Chrome 73, Edge 79, Firefox 63, Safari 12.1, Opera 60

3. Optional Catch Binding

Allows omitting the catch parameter when it is not used.

try {
  // code that might throw an error
} catch {
  // handle error without needing the error object
}

Supported since January 2020:

  • Chrome 66, Edge 79, Firefox 58, Safari 11.1, Opera 53

4. Array.flat() and Array.flatMap()

Array.flat() flattens nested arrays to a specified depth, and Array.flatMap() maps each element using a mapping function, then

const nestedArray = [[1, 2], [3, 4], [5, 6]];
const flatArray = nestedArray.flat(); // [1, 2, 3, 4, 5, 6]

const nums = [1, 2, 3, 4];
const doubledFlatArray = nums.flatMap(x => [x * 2]); // [2, 4, 6, 8]

Supported since January 2020:

  • Chrome 69, Edge 79, Firefox 62, Safari 12, Opera 56

5. Stable Array.sort()

Ensures that sorting is stable, meaning that elements with equal sort keys retain their relative order.

const products = [
  {name: "X00", price: 100},
  {name: "X01", price: 100},
  {name: "X02", price: 100},
  {name: "X03", price: 100}
];
products.sort((a, b) => a.price - b.price);
// Elements with the same price remain in their original order

6. Revised JSON.stringify()

Enhancements allow for correct handling of certain Unicode characters and improve consistency across different environments.

let jsonString = JSON.stringify("\u26D4");
console.log(jsonString); // Outputs correctly encoded character

7. Separator Symbols in String Literals Line and paragraph separators are now valid within string literals, easing certain text processing tasks.

let stringWithSeparator = "\u2028";

8. Revised Function.toString()

Returns the exact textual representation of a function, including whitespace and comments.

function exampleFunction(param) {
  // Example comment
  return param * param;
}
console.log(exampleFunction.toString());
// Outputs the function exactly as it is defined, including the comment

These features make JavaScript more robust, versatile, and consistent, facilitating development across various applications.

Take a look into your desired course