JavaScript Array Functions (#shorts)
Hi Friends,
JavaScript Array Functions
JavaScript provides many useful built-in functions for working with arrays. Here are some examples of commonly used array functions:
push(): adds one or more elements to the end of an array.
let fruits = ['apple', 'banana'];
fruits.push('orange', 'pear');
console.log(fruits); // ["apple", "banana", "orange", "pear"]
pop(): removes the last element from an array and returns it.
let fruits = ['apple', 'banana', 'orange'];
let lastFruit = fruits.pop();
console.log(lastFruit); // "orange"
console.log(fruits); // ["apple", "banana"]
shift(): removes the first element from an array and returns it.
let fruits = ['apple', 'banana', 'orange'];
let firstFruit = fruits.shift();
console.log(firstFruit); // "apple"
console.log(fruits); // ["banana", "orange"]
unshift(): adds one or more elements to the beginning of an array.
let fruits = ['apple', 'banana'];
fruits.unshift('orange', 'pear');
console.log(fruits); // ["orange", "pear", "apple", "banana"]
slice(): returns a new array containing a portion of the original array.
let fruits = ['apple', 'banana', 'orange', 'pear'];
let citrus = fruits.slice(2);
console.log(citrus); // ["orange", "pear"]
splice(): changes the contents of an array by removing or replacing existing elements and/or adding new elements.
let fruits = ['apple', 'banana', 'orange', 'pear'];
fruits.splice(1, 2, 'grape', 'melon');
console.log(fruits); // ["apple", "grape", "melon", "pear"]
concat(): merges two or more arrays and returns a new array.
let fruits = ['apple', 'banana'];
let moreFruits = ['orange', 'pear'];
let allFruits = fruits.concat(moreFruits);
console.log(allFruits); // ["apple", "banana", "orange", "pear"]
indexOf(): returns the index of the first occurrence of a specified value in an array, or -1 if it is not found.
let fruits = ['apple', 'banana', 'orange'];
let orangeIndex = fruits.indexOf('orange');
console.log(orangeIndex); // 2
filter(): returns a new array containing all elements that pass a specified test.
let numbers = [1, 2, 3, 4, 5];
let evenNumbers = numbers.filter(function(number) {
return number % 2 === 0;
});
console.log(evenNumbers); // [2, 4]
map(): returns a new array containing the results of calling a function on each element in the original array.
let numbers = [1, 2, 3, 4, 5];
let squaredNumbers = numbers.map(function(number) {
return number * number;
});
console.log(squaredNumbers); // [1, 4, 9, 16, 25]
#javascript #array #arrayfunctions #function #functions #javascriptarrayfunction #javascriptarrayfunctions #shorts #short #shortsvideo #shortsfeed #shortfeed #shortvideo #shortsyoutube #shortviral #push #pop #shift #unshift #slice #splice #concat #indexof #filter #map #princecheema_official @princecheema_official
-
1:05:02
The Breanna Morello Show
11 hours agoWhere's Joe Biden? - Luke Ball; Voters: Biden’s Mental Decline is a ‘Threat’ to National Security - Mark Mitchell; Secret Service Director's Resignation Isn't Enough! | The Breanna Morello Show
58.8K17 -
2:08:18
The Big Migâ„¢
12 hours agoAmerica’s Future, Save Our Children w, Lara Logan & Mary Flynn
75.1K11 -
1:07:47
Man in America
10 hours agoWhy Are Elites FREAKING OUT to Build Bunkers by November? w/ Jonathan Hollerman
123K99 -
1:07:45
Redacted News
10 hours agoBREAKING! NATIONAL EMERGENCY, BIDEN COUP CONFIRMED, OBAMA RESPONSIBLE | Redacted w Clayton Morris
219K706 -
30:56
Alison Morrow
9 hours agoBEST election memes 2024 circus
122K49 -
1:06:43
Awaken With JP
1 day agoThe Poop Has Hit the Fan - LIES Ep. 49
192K216 -
1:08:54
Digital Social Hour
2 days agoEgypt’s Pyramids: The SHOCKING Truth Revealed! I Robert Edward Grant
89.6K33 -
1:02:53
In The Litter Box w/ Jewels & Catturd
1 day agoThe Democrat Coup | In the Litter Box w/ Jewels & Catturd – Ep. 610 – 7/23/2024
123K86 -
1:12:28
Tate Speech by Andrew Tate
1 day agoEMERGENCY MEETING EPISODE 62 - HUGE CASE UPDATE
381K441 -
1:43:17
Fresh and Fit
13 hours agoEMERGENCY BROADCAST: USSS Director Cheatle resigns & Police Testimony on Trump Assassination
148K64