JavaScript ES10 (2019)

JavaScript ES10, also known as ECMAScript 2019, brought several enhancements that help developers write cleaner, safer, and more efficient code. While not as dramatic as ES6, the ES10 update added very useful features for working with arrays, strings, objects, and errors.

This tutorial covers:

  1. Overview of ES10
  2. Key features with examples
  3. A model mini-project using ES10 features

🔍 What’s New in ES10?

Key ES10 features:

  • Array.prototype.flat()
  • Array.prototype.flatMap()
  • Object.fromEntries()
  • Optional catch binding
  • String.prototype.trimStart() and trimEnd()
  • Symbol.prototype.description

1. 🔽 Array.prototype.flat()

The flat() method flattens nested arrays into a single array.

🧪 Example:

javascriptCopyEditconst numbers = [1, 2, [3, 4, [5, 6]]];
console.log(numbers.flat()); // [1, 2, 3, 4, [5, 6]]
console.log(numbers.flat(2)); // [1, 2, 3, 4, 5, 6]

You can control how deep it flattens with a depth parameter.


2. 🔄 Array.prototype.flatMap()

This combines map() and flat() into one method.

🧪 Example:

javascriptCopyEditconst words = ["hello", "world"];
const result = words.flatMap(word => word.split(""));
console.log(result); // ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']

3. 🏗 Object.fromEntries()

This method converts an array of key-value pairs into an object. It’s the reverse of Object.entries().

🧪 Example:

javascriptCopyEditconst entries = [["name", "Alice"], ["age", 25]];
const user = Object.fromEntries(entries);
console.log(user); // { name: "Alice", age: 25 }

This is especially useful when working with form data or query parameters.


4. 🛑 Optional Catch Binding

Before ES10, you had to specify an error parameter in catch, even if you didn’t use it.

Before:

javascriptCopyEdittry {
  throw new Error("Oops!");
} catch (error) {
  console.log("Caught an error.");
}

ES10:

javascriptCopyEdittry {
  throw new Error("Oops!");
} catch {
  console.log("Caught an error without using the variable.");
}

5. ✂️ String.prototype.trimStart() and trimEnd()

These methods remove whitespace from the beginning or end of a string.

javascriptCopyEditconst text = "   hello   ";
console.log(text.trimStart()); // "hello   "
console.log(text.trimEnd());   // "   hello"

They are more specific versions of trim().


6. 🧙 Symbol.prototype.description

You can now directly access the description of a Symbol.

javascriptCopyEditconst sym = Symbol("mySymbol");
console.log(sym.description); // "mySymbol"

🛠 Model Project: Clean & Convert User Input

🎯 Objective:

Build a script that:

  • Trims and cleans up input
  • Flattens and maps user interests
  • Converts data entries to objects
  • Handles errors without an error variable

✅ ES10 Code Model:

javascriptCopyEdit// Simulated user input
const rawInput = [
  [" name ", " Alice "],
  [" interests ", [" coding ", " music ", [" gaming "] ]],
];

// Clean and format input
try {
  const cleanedEntries = rawInput.map(([key, value]) => {
    const cleanKey = key.trim();
    const cleanValue = Array.isArray(value)
      ? value.flat(2).flatMap(item => item.trim().split(" "))
      : value.trim();
    return [cleanKey, cleanValue];
  });

  const userObject = Object.fromEntries(cleanedEntries);
  console.log("User Object:", userObject);
} catch {
  console.log("Something went wrong while processing input.");
}

🖥 Output:

javascriptCopyEditUser Object: {
  name: "Alice",
  interests: ["coding", "music", "gaming"]
}

This model uses:

  • trimStart() / trimEnd() (through trim())
  • flat() and flatMap()
  • Object.fromEntries()
  • Optional catch binding

🧠 Summary Table

FeatureDescriptionExample
flat()Flattens nested arrays[1, [2], [3, [4]]].flat(2)
flatMap()Combines map() and flat()arr.flatMap(fn)
Object.fromEntries()Converts entries to an objectObject.fromEntries([[]])
Optional catch bindingSkip unused error variablecatch { ... }
trimStart() / trimEnd()Removes leading/trailing whitespace" text ".trimStart()
Symbol.descriptionGets a symbol’s descriptionSymbol("desc").description

✅ Conclusion

ES10 (2019) made JavaScript more flexible, especially for working with data transformation, error handling, and array manipulation. Whether you’re dealing with user input or large datasets, these tools simplify many common tasks.

Learning and using ES10 features in your projects ensures that your code is modern, clean, and efficient.