๐ง๐ต๐ฟ๐ฒ๐ฒ ๐ ๐ผ๐ฑ๐ฒ๐ฟ๐ป ๐๐ฎ๐๐ฎ๐ฆ๐ฐ๐ฟ๐ถ๐ฝ๐ ๐๐ฒ๐ฎ๐๐๐ฟ๐ฒ๐ JavaScript has changed a lot in recent years. You now have cleaner ways to do common coding tasks. Three useful additions are:
- structuredClone()
- Array.prototype.at()
- Array.prototype.findLast() These features can make your code easier to read and less prone to errors.
You can use structuredClone() to create a deep copy of an object. This is better than the old way of using JSON.parse(JSON.stringify(obj)). The old way breaks when dealing with complex data types. structuredClone() preserves many built-in JavaScript types, including Dates and circular references.
You can use at() to access the last element of an array. This is cleaner than the old way of using arr[arr.length - 1]. You can use negative indexes to count from the end of the array. For example:
- arr.at(-1)
- arr.at(-2)
- arr.at(-3)
You can use findLast() to find the last item in an array that matches a condition. This is simpler than the old way of reversing the array and using find(). For example, you can use users.findLast(u => u.active) to find the last active user.
These new features can improve your everyday coding. You can learn more about them and stay up-to-date with the latest JavaScript developments. Source: https://dev.to/zamfir80/three-modern-javascript-features-that-replace-older-patterns-1ogm