DEV Community

Cover image for Essential JavaScript Methods Every Developer Should Know
Snehal Rajeev Moon
Snehal Rajeev Moon

Posted on • Updated on

Essential JavaScript Methods Every Developer Should Know

Hello Artisans,

In today's blog post is about the JavaScript ES6 (ECMAScript 2015) introduced a plenty of new features and syntax enhancements that revolutionized the way developers write JavaScript code. Among these additions are numerous methods that simplify common tasks and streamline development workflows.

So let's dive into some of the most frequently used ES6 methods, along with practical examples for each.

1. map()
The map() method creates a new array by applying a function to each element of the original array.

const numbers = [1, 2, 3, 4, 5];
const multiply= numbers.map(num => num * 2);

console.log(multiply); // Output: [2, 4, 6, 8, 10]
Enter fullscreen mode Exit fullscreen mode

2. filter()
The filter() method creates a new array with elements that pass a certain condition.

const numbers = [1, 2, 3, 4, 5];
const evens = numbers.filter(num => num % 2 === 0);

console.log(evens); // Output: [2, 4]
Enter fullscreen mode Exit fullscreen mode

3. reduce()
The reduce() method applies a function against an accumulator and each element in the array to reduce it to a single value.

const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);

console.log(sum); // Output: 15
Enter fullscreen mode Exit fullscreen mode

4. find()
The find() method returns the first element in the array that satisfies a provided testing function.

const users = [
  { id: 1, name: 'Snehal' },
  { id: 2, name: 'Rajeev' },
  { id: 3, name: 'Moon' }
];
const user = users.find(user => user.id === 2);

console.log(user); // Output: { id: 2, name: 'Rajeev' }
Enter fullscreen mode Exit fullscreen mode

5. includes()
The includes() method (Array.prototype.includes()) determines whether an array includes a certain value among its entries.

const numbers = [1, 2, 3, 4, 5, 14];
const includesFive = numbers.includes(5);

console.log(includesFive); // Output: true
Enter fullscreen mode Exit fullscreen mode

6. includes()
Similarly, includes() method (String.prototype.includes()) can be used with strings to check for the presence of a substring.

const str = 'Hello Artisans!, I am Rajeev Moon.';
const isStringIncluded = str.includes('Artisans');

console.log(isStringIncluded ); // Output: true
Enter fullscreen mode Exit fullscreen mode

7. Object.keys()
The Object.keys() method returns an array of a given object's own enumerable property names.

const person = {
  name: 'Rajeev Moon',
  age: 30,
  job: 'Developer'
};
const keys = Object.keys(person);

console.log(keys); // Output: ['name', 'age', 'job']
Enter fullscreen mode Exit fullscreen mode

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

const person = {
  name: 'Rajeev Moon',
  age: 30,
  job: 'Developer'
};
const entries = Object.entries(person);

console.log(entries); // Output: [['name', 'Rajeev Moon'], ['age', 30], ['job', 'Developer']]
Enter fullscreen mode Exit fullscreen mode

9. Arrow Functions (=>)
A concise way to write anonymous functions, providing a more readable syntax and lexical scoping of this.

const add = (a, b) => a + b;
Enter fullscreen mode Exit fullscreen mode

10. Template Literals
Enable string interpolation and multiline strings using backticks.

// backticks (``)
const name = 'Rajeev Moon';
const greeting = `Hello, ${name}! nice to meet you.`; 
console.log(greeting ); // Output: Hello Rajeev Moon nice to meet you.
Enter fullscreen mode Exit fullscreen mode

11. Destructuring Assignment
Easily extract values from arrays and objects into variables.

const person = { name: 'Rajeev Moon', age: 30 };
const { name, age } = person;
Enter fullscreen mode Exit fullscreen mode

12. Spread Syntax (...)
Expand iterable objects like arrays or objects into individual elements.

const numbers = [1, 2, 3];
const newNumberList = [...numbers, 4, 5]; 
console.log(newNumberList ); // Output: [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

13. Rest Parameters (...)
Gather remaining function arguments into an array.

function sum(...args) {
  return args.reduce((acc, val) => acc + val, 0);
}
Enter fullscreen mode Exit fullscreen mode

14. Object Literal Enhancements
Shorter syntax for defining object properties.

const x = 10, y = 20;
const point = { x, y }; // { x: 10, y: 20 }
Enter fullscreen mode Exit fullscreen mode

These are just a few of the many ES6 features and methods that have become integral parts of modern JavaScript development.

Understanding and utilizing these methods of javascript can greatly improve the readability, maintainability, and efficiency of your code.

Buy Me A Coffee

Happy Reading, Happy Coding!! ❤️ 🦄

Top comments (6)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

The map, filter, and reduce methods on Arrays have been available since ES5 (2009)

Collapse
 
shifi profile image
Shifa Ur Rehman

Nevertheless everyone should know about them 😂

Collapse
 
adderek profile image
Maciej Wakuła

Async, await, for, in, of, const, let, try,?., and many more. Npm, how to avoid main thread locks, prototyping, jsdoc, v8,... And many more. There are just so many... Instead of listing them you should focus on the node first and it's differences from other languages. Where it shines and where it fails. How single threaded language was so effective that java had its loom project and cotlin lightweight threads (and why watchdogs often fails, especially for juniors written code). When to use Web pack and why using a single tool too much is too much. Simplicity vs complexity. What are ecmascript versions. Callback - the good, the bad and the ugly. And more. 2 years later you might focus on the language features like map and reduce. And also things like git (and it's rerere or mr)

Collapse
 
snehalkadwe profile image
Snehal Rajeev Moon

Thank you for sharing your thoughts. I appreciate your suggestions and take your feedback into account for future blog topics, including a deeper dive into JavaScript's unique features, its strengths and weaknesses compared to other languages, and practical considerations like avoiding main thread locks and understanding prototyping.
I will consider your opinion on the importance of understanding tools like Git.

Thanks again for your engagement and enthusiasm! ❤️ 😃

Collapse
 
fxalvarezd profile image
fxalvarezd

On #7, output should be [name, age, job], correct?

Collapse
 
snehalkadwe profile image
Snehal Rajeev Moon

@fxalvarezd Ohh, sorry my mistake. Yes for #7 the output will be [name, age, job] as Object.keys() give us the name of the keys.

I have updated that. 😃