ES6 - Object Extensions



String extension

Some popular methods added to the String object in ES6 are −

Sr.NoMethod & Description
1str.startsWith(searchString[, position])

determines whether a string begins with the characters of a specified string. Returns true or false

2str.endsWith(searchString[, length])

determines whether a string ends with the characters of a specified string. Returns true/false

3str.includes(searchString[, position])

determines whether one string may be found within another string

4str.repeat(count)

constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together

Regex extensions

In a regular expression, for example, /[A-Z]/g, the beginning and ending / are called delimiters. Anything after the closing delimiter is called a modifier. ES6 adds a new modifier /g where g stands for global. This match all instances of the pattern in a string, not just one.

Example

The following example searches and returns all upper-case characters in the string.

<script>
   let str = 'JJavascript is Fun to Work , very Fun '
   let regex = /[A-Z]/g // g stands for global matches
   let result = str.match(regex);
   console.log(result)
</script>

The output of the above code will be as given below −

["J", "J", "F", "W", "F"]

Regular expression searches are case-sensitive. To turn-off case-sensitivity, use the /i modifier.

Example

The following example performs a case insensitive global match. The example replaces fun with enjoyable.

<script>
   // /gi global match ignore case

   let str = 'Javascript is fun to Work , very Fun '
   let regex = /Fun/gi;
   console.log(str.replace(regex,'enjoyable'));
   console.log(str)
   console.log(str.search(regex))
</script>

The output of the above code will be as shown below −

Javascript is enjoyable to Work , very enjoyable
Javascript is fun to Work , very Fun
15

Number

Some popular methods added to the Number object in ES6 are −

Sr.NoMethod & Description
1Number.isFinite(value)

method determines whether the passed value is a finite number. Returns true/false.

2Number.isNaN(value)

returns true if the given value is NaN and its type is Number; otherwise, false.

3Number.parseFloat(string)

A floating-point number parsed from the given value. If the value cannot be converted to a number, NaN is returned.

4Number.parseInt(string,[ radix])

method parses a string argument and returns an integer of the specified radix or base.

Math

Some popular methods added to the Math object in ES6 are −

Sr.NoMethod & Description
1Math.sign()

function returns the sign of a number, indicating whether the number is positive, negative or zero.

2Math.trunc()

function returns the integer part of a number by removing any fractional digits.

Methods of Array in ES6

The table given below highlights the different array methods in ES6 along with the description.

Sr.NoMethod & Description
1copyWithin()

shallow copies part of an array to another location in the same array and returns it without modifying its length.

2entries()

method returns a new Array Iterator object that contains the key/value pairs for each index in the array.

3find()

method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned..

4fill()

method fills all the elements of an array from a start index to an end index with a static value. It returns the modified array.

5Array.of()

method creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments.

6Array.from()

method creates a shallow copy from an array like or iterable object.

Object

Methods related to Object function are mentioned below in the table along with the respective description.

Sr.NoMethod & Description
1Object.is()

method determines whether two values are the same value

2Object.setOf()

method sets the of a specified object to another object or null.

3Object.assign()

method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.