Programmers are always looking for new ways to make their lives easier, and I’m no different. That’s why I will occasionally find myself perusing the latest ECMAScript specification for features that may help me with my day-to-day. Today I’d like to draw some attention to some of the newest features that I believe will have a significant impact on the way we write code in JS.
The .at() method allows for the use of “negative indexing” of basic JS iterables (Array, String, TypedArray). This is a feature in other languages, like Python, and is something JS developers have been asking for for quite some time now.
Why doesn’t negative indexing an array work when using bracket notation? This is because the [ ] syntax is not exclusive to Arrays and Strings. It applies to all objects. Referencing arr[-1] yields undefined, because you are really trying to reference the property of an object with a key of “-1”.
Having to name the indexable class twice has always felt a bit clunky to me. In addition to being overly wordy, this method does not lend itself to indexables returned by a function.
Negative indexing is supported by the .slice method (as well as .splice) and functions as desired.
Using the .slice method also gets rid of the need to store an array returned from a function in a variable before referencing it.
What to note here is that the return value of .slice is itself an array, and so we need to append the . While this syntax is more flexible than the first, I still believe it is not the most readable.
Let’s see how this would be achieved with the .at() method.
Terse. Flexible. Readable.
The new Object.hasOwn() method is meant to be a replacement for Object.hasOwnProperty(). It is used to check if a property exists on a given object.
The main difference between the two methods is that Object.hasOwn() only checks for properties owned by a given object, and ignores properties inherited from Object.prototype. This is useful because methods on Object.prototype may not always be available.
Such is the case for objects created with Object.create(null)
The added benefit of this is that it negates the need for third-party implementations like lodash.has.
I am a big proponent of readability, so the addition of numeric separators is a feature that’s had me pretty excited. Numeric separators allow for the use of an _ to create a grouping of integers making it easier for developers to quickly decipher large numbers.
The new ECMAScript specification introduces a set of logical assignment operators similar to ones found in other languages like Ruby, and can save you a few lines of code.
Assigns when the value to the left of the operator is falsey.
Assigns when the value to the left of the operator is truthy.
Assigns when the value to the left of the operator is nullish (null or undefined).
All of the features mentioned here have reached Stage 4, and are supported by most modern browsers. Here are some links to the documentation in case you would like to learn more.
Cory Bosselmann is a full stack mobile and web engineer with a love for performant and scalable code. He has extensive startup experience in a variety of industries including Music and Entertainment, Consumer Services, and Commercial Aviation. He has spent the last four years building complex and engaging apps from the ground up.