DEV Community

Sibi🥱
Sibi🥱

Posted on

Must know concepts of JS to make life simple

Promises and Async/Await:

Handling asynchronous operations in JavaScript will be a pain, especially using callbacks. If one uses callbacks he'll find himself in callback hell. To avoid that, we use

  1. Promises - A way to handle asynchronous operations in JavaScript.

  2. Async/await - It simplifies working with Promises. It makes asynchronous code look synchronous.
    async function fetchData() {
    try {
    let response = await fetch('https://api.example.com/data');
    let data = await response.json();
    console.log(data);
    } catch (error) {
    console.error(error);
    }
    }

Modules in JavaScript:

Modules are an alternative of commonJS which is a feature of ECMA script that allows us to organize code into reusable and maintainable units by exporting and importing modules.
export
export function myFunction() {
// Your code here
}

import
import { myFunction } from (path of the export file)

Event Handling:

Handling user interactions and events like adding event listeners to buttons or forms.
const button = document.querySelector('#myButton');
button.addEventListener('click', () => {
// Your event handling code here
});

map() Function:

The map() function is a powerful and commonly used array method in JavaScript. It allows you to transform each element of an array by a function and create a new array based on the result.
const numbers = [1, 2, 3, 4, 5];
// map function to double each number
const doubledNumbers = numbers.map((number) => {
return number * 2;
});


console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10]

Top comments (0)