ES6: New Methods

15min read

Developers have now with ES6 new built-in methods to make it easier working with Strings, Arrays, Objects and Numbers.

String Methods

Three methods to search in Strings

String starts with a specific substring.

String ends with a specific substring.

String includes a specific substring.

Repeat a String

We can repeat a String now very easy with the built-in repeat method.

Array Methods

Included

Use .includes() to check if a certain value is included in an array.

Find

Use .find() to look out for a specific value in an array.

But unlike the filter method it will only find the first match.

Findindex

Use .findIndex() to determine at which index a specific value is located in an array.

Fill

The built-in fill method allows us to fill an array with values. By applying the appropriate parameters we can choose to fill the array with a specific value from and until a specific position.

Values

This new built-in method returns an array iterator of the values so we can run a for loop to extract each value of the array.

Entries

Array.entries() also returns an iterator but when looped through it returns both the key and value in an array format.

Keys

Array.keys() returns an iterator

Convert to Array

Convert strings or array-like objects to an array.

See also my article about the spread operator to learn how to convert an array-like object to an array.

Objects

Merge with Object.assign

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. If the properties of target and source object are identical the properties from the taget object get overwritten. Finally it will return the target object.

It can be also used to merge multiple objects into one new object.

Clone with Object.assign

As seen before it can be used to clone an object by providing as the target simply an empty object.

But be aware for deep cloning, we need to use other alternatives because Object.assign() copies property values. If the source value is a reference to an object, it only copies that reference value.

Get Object properties as array with Object.entries

The Object.entries() method returns an array of a given object’s own enumerable property [key, value] pairs.

Get property information with Object.getOwnPropertyDescriptors

Sometimes we need more insights about our objects own properties.

Numbers

Number.isNaN()

Number.isNaN() checks if the value passed in is NaN or not.

Number.isInteger()

Number.isInteger() checks if the value passed in is an integer or not.

Number.isSafeInteger()

Number.isSafeInteger() checks if the value passed in is a safe number. This feature is useful in validating user input.

Number.isFinite()

Number.isFinite() checks if the value passed in is a finite number or not.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *