ES6 - Object Extensions



String extension

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

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

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

2 str.endsWith(searchString[, length])

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

3 str.includes(searchString[, position])

determines whether one string may be found within another string

4 str.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.No Method & Description
1 Number.isFinite(value)

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

2 Number.isNaN(value)

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

3 Number.parseFloat(string)

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

4 Number.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.No Method & Description
1 Math.sign()

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

2 Math.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.No Method & Description
1 copyWithin()

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

2 entries()

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

3 find()

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

4 fill()

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.

5 Array.of()

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

6 Array.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.No Method & Description
1 Object.is()

method determines whether two values are the same value

2 Object.setPrototypeOf()

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

3 Object.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.

Advertisements