๐ง๐ต๐ฟ๐ฒ๐ฒ ๐ ๐ผ๐ฑ๐ฒ๐ฟ๐ป ๐๐ฎ๐๐ฎ๐ฆ๐ฐ๐ฟ๐ถ๐ฝ๐ ๐๐ฒ๐ฎ๐๐๐ฟ๐ฒ๐ JavaScript has changed a lot in recent years. You can now use cleaner, more expressive alternatives to common coding patterns. Three useful additions are:
- structuredClone()
- Array.prototype.at()
- Array.prototype.findLast() These features can make your code easier to read, less error-prone, and more maintainable.
Creating a deep copy of an object used to be a challenge. You had to use hacks like: const copy = JSON.parse(JSON.stringify(obj)); This approach works for simple objects, but it breaks with complex data types. A better way to make a deep copy is: const copy = structuredClone(obj); This function preserves many built-in JavaScript types, including Dates, Maps, and Sets.
Accessing the last element of an array used to look like this: const lastItem = arr[arr.length - 1]; The newer at() method provides a cleaner alternative: const lastItem = arr.at(-1); Negative indexes count from the end of the array.
You can also use findLast() to get the last active user in an array: const user = users.findLast(u => u.active); This method searches from the end of the array and returns the first matching element.
Source: https://dev.to/zamfir80/three-modern-javascript-features-that-replace-older-patterns-1ogm