are a special type of object used for storing multiple values in a single variable.
Arrays
They are ordered collections of elements, where each element is identified by an index (starting from 0).
Array
Can hold elements of different types: numbers, strings, objects, etc.
Arrays
let fruits = [‘apple’, ‘banana’, ‘orange’];
Arrays Literals
let numbers = new Array(1, 2, 3, 4, 5);
Array Constructor
let emptyArray = [];
Empty Array:
How do u add elements in the end in array
fruits.push(‘grape’);
How do u add to the beggining in Array
fruits.unshift(‘strawberry’)
Removes from the end
pop()
Removes from the beginning
shift()
Removes at a specific index
splice(index, count)
how do u loop a array
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
how do u use a forEach()
fruits.forEach(fruit => console.log(fruit));
how do you use uppercase
let uppercaseFruits = fruits.map(fruit => fruit.toUpperCase());
Finds the first occurrence
indexOf(element)
Checks if an element is present
includes(element)
Sorts elements in place
sort()
Reverses the order of elements
reverse()
how do u use filter()
let longFruits = fruits.filter(fruit => fruit.length > 5);
how do u use reduce
let totalLength = fruits.reduce((total, fruit) => total + fruit.length, 0);
the output will be the total length of letters added together in an array
document.write(totalLength);
is a collection of key-value pairs where keys are unique.
Map
how do u create a map in js
let myMap = new Map();
myMap.set(‘key1’, ‘value1’);
myMap.set(‘key2’, ‘value2’);
how do u access values of maps in js
let value = myMap.get(‘key1’);