DEV Community

Saravanan Cn
Saravanan Cn

Posted on

Day 15: Learning JavaScript Splice, Spread, Rest, Fetch, Promises & Array Methods 🚀

Today I learned several important JavaScript concepts, including array methods, spread and rest operators, fetch(), promises, arrow functions, and truthy/falsy values.

1.Splice

I learned how splice() can be used to add, remove, or replace elements in an array.

``javascript
let fruits = ["Apple", "Banana", "Orange", "Mango"];

fruits.splice(1, 1);

console.log(fruits);`

2. Spread Operator (...)

The spread operator ... is used to expand the elements of an array or properties of an object.

`let numbers = [1, 2, 3];

let newNumbers = [...numbers, 4, 5];

console.log(newNumbers);`

3. Rest Parameter (...)

The rest parameter ... collects multiple arguments into an array.

`function add(...numbers) {
console.log(numbers);
}

add(10, 20, 30);`

4. Arrow Functions

I learned that when an arrow function uses curly braces {}, we normally need a return statement if we want to return a value.

let add = (a, b) => {
return a + b;
};

Without curly braces the value will be returned automatically.

let add = (a, b) => a + b;

5. fetch()

I learned about the fetch() method, which can be used to request data from a server or API.

``fetch("https://example.com/data")
.then(response => response.json())
.then(data => console.log(data));

6. Promise

I also learned about Promises.
A Promise is a JavaScript object that represents the eventual completion or failure of an asynchronous operation.
fetch() returns a Promise, which is why we can use .then() with it.

fetch("https://example.com/data")
.then(response => response.json())
.then(data => console.log(data));

7. forEach()

I used forEach() for iteration. It executes a function once for each element in an array.

`let numbers = [1, 2, 3];

numbers.forEach(function(number) {
console.log(number);
});
`

8. filter()

I learned that filter() creates a new array containing elements that pass a condition.

`let numbers = [1, 2, 3, 4, 5];

let result = numbers.filter(number => number > 2);

console.log(result);

9. map()

I also learned about map(). It creates a new array by transforming each element.`

`let numbers = [1, 2, 3];

let result = numbers.map(number => number * 2);

console.log(result);`

10. Truthy and Falsy Values

I learned about truthy and falsy values in JavaScript.
Some falsy values include:

  • false
  • 0
  • ""
  • null
  • undefined
  • NaN Values that are not falsy are generally treated as truthy.

Top comments (0)