Yuan's Blog
EN

4 Important New Features in ES14 (ES2023)

  1. Methods for searching arrays from the end: findLast() and findLastIndex()

    • findLast() returns the value of the first element (from the end) that matches the condition. If no element matches, it returns undefined.
    • findLastIndex() returns the index of the first element (from the end) that matches the condition. If no element matches, it returns -1.
    • Note: These two methods work the same as find() and findIndex(), except they search from the end of the array. This makes it easier to get “the last matching element.”
  2. Four new non-mutating array methods

    • toReversed(): Returns a new array with the elements reversed (the original reverse() mutates the array).
    • toSorted(fn): Returns a new sorted array (the original sort() mutates the array).
    • toSpliced(start, deleteCount, ...items): Returns a new array with elements removed/inserted (the original splice() mutates the array).
    • with(index, value): Returns a new array with the element at the given index replaced.
    • Note: These four methods are designed to provide the same functionality as their mutating counterparts, but without modifying the original array (no mutation / no side effects).
  3. Hashbang Grammar

    • A Hashbang is #! followed by the interpreter path. It only works on the first line of a file and is used to specify which interpreter should run the script. Examples:
      • /usr/bin/python3
      • /usr/bin/node
      • /bin/bash
    • Before running the file, you must grant execution permission using chmod +x app.js; otherwise the hashbang will not take effect.
    • Languages like Node, Python, and Ruby do not run directly on the CPU. They require an interpreter to translate the code line by line into executable machine instructions. That “translator” is the interpreter.
    • Note: Hashbang enables JavaScript files to be executed directly like shell/Python/Ruby scripts (e.g., ./app.js), instead of only running them via node app.js.
  4. WeakMap now supports Symbol keys

    • Previously, WeakMap keys could only be objects. Starting from ES2023, Symbols can also be used as keys. This makes it easier to store globally unique, non-exposed metadata in a WeakMap, which is especially useful for library authors or scenarios requiring implicit metadata management.