DEV Community

Sathish Kumar
Sathish Kumar

Posted on

JavaScript Notes:

1.Spread Operator (...):

​Used to expand elements of an array or add new elements easily.

Example:

let arr2 = [4, 5, 6];

arr2 = [...arr2, 7]; // Adding 7 to the existing array

console.log(arr2);

// Output: [4, 5, 6, 7]

2. Array Destructuring & Rest Operator (...):

​Extracting specific elements from an array into variables, and packing the remaining elements using the Rest operator.

Example:

const arr1 = [1, 2, 3, 4, 5, 6, 7, 8];

// 'a' gets 1st value, 'b' gets 2nd value, '...c' collects the rest!

const [a, b, ...c] = arr1;

console.log(a);

// Output: 1

console.log(b); // Output: 2

console.log(c); // Output: [3, 4, 5, 6, 7, 8]

3. Fetch API & Promises:

​Used to request data from a server asynchronously.

Example:

const promise = fetch('https://api.example.com/data');

promise

.then((res) => res.json()) // Convert response to JSON

.then((data) => {

// Accessing specific nested data

console.log(data.product[0].image[0]);
Enter fullscreen mode Exit fullscreen mode

})

.catch((err) => {

// Handle errors if the request fails

console.log("Error:", err);
Enter fullscreen mode Exit fullscreen mode

});

Key Takeaway:

​Spread Operator (...): Expands elements.

​Rest Operator (...): Combines multiple elements into an array.

​Fetch API: Handles asynchronous HTTP requests using Promises.

Top comments (0)