- Published on
Essential ES6 Methods in JavaScript You Should Know
- Authors

- Name
- Alamin Sheikh
- @alaminsheikh01
JavaScript ES6 (ECMAScript 2015) introduced a host of new methods that have transformed the way developers write cleaner, more efficient code. In this post, we'll explore some of the most useful ES6 methods that every JavaScript developer should be familiar with.
1. Array.prototype.find()
The find() method returns the first element in an array that satisfies a provided testing function. If no elements satisfy the testing function, it returns undefined.
{
const numbers = [1, 2, 3, 4, 5];
const found = numbers.find((num) => num > 3);
console.log(found); // Output: 4
}
2. Array.prototype.findIndex()
Similar to find(), the findIndex() method returns the index of the first element that satisfies a testing function. If no elements satisfy the function, it returns -1.
{
const numbers = [1, 2, 3, 4, 5];
const index = numbers.findIndex((num) => num > 3);
console.log(index); // Output: 3
}
3. Array.prototype.includes()
The includes() method checks if an array contains a certain element, returning true or false as appropriate.
{
const fruits = ["apple", "banana", "mango"];
console.log(fruits.includes("banana")); // Output: true
console.log(fruits.includes("grapes")); // Output: false
}
4. String.prototype.startsWith() and endsWith()
The startsWith() and endsWith() methods check if a string begins or ends with a specified substring, returning true or false.
{
const message = "Hello World";
console.log(message.startsWith("Hello")); // Output: true
console.log(message.endsWith("World")); // Output: true
}
5. Array.prototype.map()
The map() method creates a new array by applying a function to each element in the original array.
{
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map((num) => num * 2);
console.log(doubled); // Output: [2, 4, 6, 8, 10]
}
6. Array.prototype.filter()
The filter() method creates a new array containing elements that satisfy a given condition.
{
const numbers = [1, 2, 3, 4, 5];
const even = numbers.filter((num) => num % 2 === 0);
console.log(even); // Output: [2, 4]
}
7. Object.assign()
The Object.assign() method copies all enumerable properties from one or more source objects to a target object.
{
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(returnedTarget); // Output: { a: 1, b: 4, c: 5 }
}
Conclusion
These ES6 methods can significantly enhance your JavaScript code by making it more readable, concise, and efficient. Getting comfortable with these methods will improve your coding skills and help you write better, modern JavaScript.