Reverse & Join
Lesson 9 of 18 in Coddy's Array Methods in JavaScript course.
The Array.reverse() method is a built-in function in JavaScript that directly modifies the original array, reversing the order of its elements. It starts from the first element and swaps it with the last element, then the second element with the second-to-last element, and so on, until the entire array is reversed.
You can simply call the reverse() method on an array variable. The syntax is:
myArray.reverse();let numbers = [1, 2, 3, 4, 5];
let strng = ['A', 'B', 'C', 'D'];
// To reverse the items, use the reverse() method as shown below.
numbers.reverse();
console.log(numbers); // Output: [5, 4, 3, 2, 1]
strng.reverse();
console.log(strng); // Output: ['D', 'C', 'B', 'A']The Array.join() method is a built-in function that operates on arrays in JavaScript. It takes all the elements within an array and combines them into a single string. You can optionally specify a separator string to insert between each element in the resulting string. If no separator is provided, the default separator is a comma (","). For example:
let strArray = ['Y', 'A', 'R', 'R', 'A'];
strArray.reverse();
console.log(strArray.join()); // Output: 'A,R,R,A,Y'
console.log(strArray.join("")); // Output: 'ARRAY'
console.log(strArray.join("-")); // Output: 'A-R-R-A-Y'Challenge
EasyAn array called secretWord is given, containing string elements.
Write JavaScript code to reverse the array and log the hidden word using the appropriate join() separator.
Try it yourself
let secretWord = ['h', 'c', 'e', 't', 'y', 'd', 'd', 'o', 'c'];
// Write code here