ES6 Methods in JavaScript

ES6 (ECMAScript 2015) introduced several new methods for working with arrays, strings, objects, and more. These methods enhance the functionality of JavaScript, making it more efficient and readable. Here’s a summary of some key ES6 methods:

1. Array Methods

Array.from(): Creates a new array from an array-like or iterable object.
javascript
Copy code
const str = 'hello';

const arr = Array.from(str); // ['h', 'e', 'l', 'l', 'o']



Array.of(): Creates a new array with the given arguments.
javascript
Copy code
const arr = Array.of(1, 2, 3); // [1, 2, 3]



Array.prototype.find(): Returns the first element in an array that satisfies the provided testing function.

javascript
Copy code
const arr = [5, 12, 8, 130, 44];

const found = arr.find(element => element > 10); // 12



Array.prototype.findIndex(): Returns the index of the first element in an array that satisfies the provided testing function.
javascript
Copy code
const arr = [5, 12, 8, 130, 44];

const index = arr.findIndex(element => element > 10); // 1



Array.prototype.includes(): Checks if an array contains a certain element, returning true or false.
javascript
Copy code
const arr = [1, 2, 3];

arr.includes(2); // true



Array.prototype.entries(): Returns an iterator object with key/value pairs for each index in the array.
javascript
Copy code
const arr = ['a', 'b', 'c'];

for (let [index, element] of arr.entries()) {

  console.log(index, element);

}

// Output: 

// 0 'a'

// 1 'b'

// 2 'c'



2. String Methods

String.prototype.includes(): Determines whether a string contains the characters of a specified string.
javascript
Copy code
const str = 'Hello, world!';

str.includes('world'); // true



String.prototype.startsWith(): Determines whether a string begins with the characters of a specified string.
javascript
Copy code
const str = 'Hello, world!';

str.startsWith('Hello'); // true



String.prototype.endsWith(): Determines whether a string ends with the characters of a specified string.
javascript
Copy code
const str = 'Hello, world!';

str.endsWith('!'); // true



String.prototype.repeat(): Repeats the string a specified number of times.
javascript
Copy code
const str = 'Hello';

str.repeat(3); // 'HelloHelloHello'



3. Object Methods

Object.assign(): Copies the values of all enumerable properties from one or more source objects to a target object.
javascript
Copy code
const target = { a: 1 };

const source = { b: 2, c: 3 };

const result = Object.assign(target, source); // { a: 1, b: 2, c: 3 }



Object.entries(): Returns an array of a given object’s own enumerable property [key, value] pairs.
javascript
Copy code
const obj = { a: 1, b: 2, c: 3 };

Object.entries(obj); // [['a', 1], ['b', 2], ['c', 3]]



Object.values(): Returns an array of a given object's own enumerable property values.
javascript
Copy code
const obj = { a: 1, b: 2, c: 3 };

Object.values(obj); // [1, 2, 3]



Object.keys(): Returns an array of a given object's own enumerable property names.
javascript
Copy code
const obj = { a: 1, b: 2, c: 3 };

Object.keys(obj); // ['a', 'b', 'c']



4. Number Methods

Number.isNaN(): Determines whether the passed value is NaN (Not-a-Number) and its type is Number.
javascript
Copy code
Number.isNaN(NaN); // true

Number.isNaN('hello'); // false



Number.isFinite(): Determines whether the passed value is a finite number.
javascript
Copy code
Number.isFinite(10); // true

Number.isFinite(Infinity); // false



5. Promise Methods

Promise.all(): Waits for all promises to be resolved or for any to be rejected.
javascript
Copy code
const promise1 = Promise.resolve(3);

const promise2 = 42;

const promise3 = new Promise((resolve, reject) => {

  setTimeout(resolve, 100, 'foo');

});


Promise.all([promise1, promise2, promise3]).then(values => {

  console.log(values); // [3, 42, 'foo']

});



Promise.race(): Returns a promise that resolves or rejects as soon as one of the promises in the array resolves or rejects.
javascript
Copy code
const promise1 = new Promise((resolve, reject) => {

  setTimeout(resolve, 500, 'one');

});


const promise2 = new Promise((resolve, reject) => {

  setTimeout(resolve, 100, 'two');

});


Promise.race([promise1, promise2]).then(value => {

  console.log(value); // 'two'

});



These methods and features significantly improve the flexibility, readability, and efficiency of JavaScript code.

No comments:

Post a Comment