Array Methods Flashcards
Learn Array Methods
CONCAT() METHOD
- What does the concat() method do?
- Does it change the existing arrays?
- Does it return a new array?
Concat - Question 1/5
The concat() method is used to merge/combine two or more arrays.
This method does not change the existing arrays, but instead returns a new array.
DOCUMENTATION LINK
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
CONCAT() METHOD
Does concat() return a new array?
Concat - Question 2/5
Yes
EXAMPLEconst array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);
// Expected output: Array [“a”, “b”, “c”, “d”, “e”, “f”]
CONCAT() METHOD
Is concat() a copy method or a mutating method?
Concat - Question 3/5
Copy method.
Concat() does NOT mutate/alter the original/source arrays that it’s concatenating from.
CONCAT() METHOD
Can you concat more than 2 arrays?
More than 3, 4, etc?
Concat - Question 4/5
Yes
The concat() method allows you to merge/combine as many arrays as you want.
CONCAT() METHOD
Which is the correct syntax to write a concat() method?
A: concat(num1, num2)
B: num1.concat(num2)
C: num1.concat.num2
D: concat(num1).num2
Concat - Question 5/5
Bnum1.concat(num2)
You input the initial array followed by the concat method. You then input the arrays you want to concat with the initial array as arguments separated by commas.
JOIN() METHOD
- What does the join() method do?
- Does it change the existing arrays?
- Does it return a new array?
- Can you use integers too, not just strings?
Join - Question 1/5
The join() method CREATES and RETURNS a NEW STRING by concatenating all of the elements in an array, separated by commas or a specified separator string.
Yes, you can use integers.
DOCUMENTATION LINK
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
JOIN() METHOD
Does join() return a new array?
Join - Question 2/5
Yes
const words = ['Tyler', 'Kristen', 'Jack', 'Jill']
const joinedWords = words.join();
console.log(joinedWords);
//output -> Tyler,Kristen,Jack,Jill
JOIN() METHOD
Is join() a copy method or a mutating method?
Join - Question 3/5
Copy method.
Join() does NOT mutate/alter the original/source array that it’s joining from.
JOIN() METHOD
Which is the correct syntax to write a join() method?
A: join.words()
B: join(words)
C: words.join()
D: words.join(words)
Join - Question 4/5
Cwords.join()
You input the array name followed by the join() method. You then specify in its argument a separator string if you want.
words.join('')
// Output -> WordWordWord
If there is no specified separator string, it will default to separating the joined strings with un-spaced commas.
words.join()
// Output -> Word,Word,Word
JOIN() METHOD
What’s a separator string?
Join - Question 5/5
A separator string is the argument you provide the join() method to separate the joined strings.
This affects how the new array returns the data.
EXAMPLES
Nothing separates the strings with un-spaced commas by defaultjoin()
//Output -> Word,Word,Word
Empty apostrophes (or quotes) returns the strings next to each other with no spacingjoin('')
//Output -> WordWordWord
Apostrophes (or quotes) with a space in between separates the strings with a spacejoin(' ')
//Output -> Word Word Word
Symbols (such as ,-_+& etc…) WITHOUT spacing separate the strings with that symboljoin('+')
//Output -> Word+Word+Word
Symbols WITH spacing separate the strings with the symbol and spacingjoin(', ')
//Output -> Word, Word, Word
You can even add a space before and after a symboljoin(' + ')
//Output -> Word + Word + Word
SHIFT() METHOD
- What does the shift() method do?
- Does it change the existing arrays?
Shift - Question 1/6
The shift() method REMOVES the FIRST ELEMENT from an array and RETURNS that REMOVED ELEMENT.
This method CHANGES the length of THE ARRAY.
Note: The pop() method is identical to the shift() method, except pop() removes the last element of an array instead.
DOCUMENTATION LINK
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
SHIFT() METHOD
Does shift() return a new array?
Shift - Question 2/6
No.
It does not return a new array, but rather mutates the original array.
It does however return the removed value/element.
You can gain access to the returned removed element by storing it in a variable.
SHIFT() METHOD
Is shift() a copy method or a mutating method?
Shift - Question 3/6
Mutating method.
Shift() will directly change/alter the original source array that the method is being performed on.
If you console.log the source array after the method has been performed, the unshift() changes to that array will be apparent.
SHIFT() METHOD
How do you access the returned removed element?
Shift - Question 4/6
You can assign the method operation to a variable.
That variable will then store, or rather become that removed element.
EXAMPLEconst array = [1, 2, 3]
const removedElement = array.shift()
console.log(removedElement);
// Output -> 1`
SHIFT() METHOD
What will A & B console.log?
const array = [1, 2, 3]
const removedElement = array.shift();
A: console.log(array);
B:console.log(removedElement);
Shift - Question 5/6
A:
[2. 3]
B:
1
Notice how the returned removed element is no longer in the array.
It does not create a new array.
It just returns the removed element itself; whether that be an integer, string, etc.