curriculum/challenges/english/blocks/review-javascript-arrays/6723c66f623701a3cdf72130.md
const developers = ["Jessica", "Naomi", "Tom"];
undefined.:::interactive_editor
const developers = ["Jessica", "Naomi", "Tom"];
console.log(developers[0]) // "Jessica"
console.log(developers[1]) // "Naomi"
console.log(developers[10]) // undefined
:::
length Property: This property is used to return the number of items in an array.:::interactive_editor
const developers = ["Jessica", "Naomi", "Tom"];
console.log(developers.length) // 3
:::
=) to assign a new value to the element at a specific index.:::interactive_editor
const fruits = ['apple', 'banana', 'cherry'];
fruits[1] = 'blueberry';
console.log(fruits); // ['apple', 'blueberry', 'cherry']
:::
:::interactive_editor
const chessboard = [
['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'],
['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'],
['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r']
];
console.log(chessboard[0][3]); // "Q"
:::
:::interactive_editor
const fruits = ["apple", "banana", "orange"];
const [first, second, third] = fruits;
console.log(first); // "apple"
console.log(second); // "banana"
console.log(third); // "orange"
:::
:::interactive_editor
const fruits = ["apple", "banana", "orange", "mango", "kiwi"];
const [first, second, ...rest] = fruits;
console.log(first); // "apple"
console.log(second); // "banana"
console.log(rest); // ["orange", "mango", "kiwi"]
:::
push() Method: This method is used to add elements to the end of the array and will return the new length.:::interactive_editor
const desserts = ["cake", "cookies", "pie"];
desserts.push("ice cream");
console.log(desserts); // ["cake", "cookies", "pie", "ice cream"];
:::
pop() Method: This method is used to remove the last element from an array and will return that removed element. If the array is empty, then the return value will be undefined.:::interactive_editor
const desserts = ["cake", "cookies", "pie"];
desserts.pop();
console.log(desserts); // ["cake", "cookies"];
:::
shift() Method: This method is used to remove the first element from an array and return that removed element. If the array is empty, then the return value will be undefined.:::interactive_editor
const desserts = ["cake", "cookies", "pie"];
desserts.shift();
console.log(desserts); // ["cookies", "pie"];
:::
unshift() Method: This method is used to add elements to the beginning of the array and will return the new length.:::interactive_editor
const desserts = ["cake", "cookies", "pie"];
desserts.unshift("ice cream");
console.log(desserts); // ["ice cream", "cake", "cookies", "pie"];
:::
indexOf() Method: This method is useful for finding the first index of a specific element within an array. If the element cannot be found, then it will return -1.:::interactive_editor
const fruits = ["apple", "banana", "orange", "banana"];
const index = fruits.indexOf("banana");
console.log(index); // 1
console.log(fruits.indexOf("not found")); // -1
:::
splice() Method: This method is used to add or remove elements from any position in an array. The return value for the splice() method will be an array of the items removed from the array. If nothing is removed, then an empty array will be returned. This method will mutate the original array, modifying it in place rather than creating a new array. The first argument specifies the index at which to begin modifying the array. The second argument is the number of elements you wish to remove. The following arguments are the elements you wish to add.:::interactive_editor
const colors = ["red", "green", "blue"];
colors.splice(1, 0, "yellow", "purple");
console.log(colors); // ["red", "yellow", "purple", "green", "blue"]
:::
includes() Method: This method is used to check if an array contains a specific value. This method returns true if the array contains the specified element, and false otherwise.:::interactive_editor
const programmingLanguages = ["JavaScript", "Python", "C++"];
console.log(programmingLanguages.includes("Python")); // true
console.log(programmingLanguages.includes("Perl")); // false
:::
concat() Method: This method creates a new array by merging two or more arrays.:::interactive_editor
const programmingLanguages = ["JavaScript", "Python", "C++"];
const newList = programmingLanguages.concat("Perl");
console.log(newList); // ["JavaScript", "Python", "C++", "Perl"]
:::
slice() Method: This method returns a new array containing a shallow copy of a portion of the original array, specified by start and end indices. The new array contains references to the same elements as the original array (not duplicates). This means that if the elements are primitives (like numbers or strings), the values are copied; but if the elements are objects or arrays, the references are copied, not the objects themselves.:::interactive_editor
const programmingLanguages = ["JavaScript", "Python", "C++"];
const newList = programmingLanguages.slice(1);
console.log(newList); // ["Python", "C++"]
:::
:::interactive_editor
const originalArray = [1, 2, 3];
const shallowCopiedArray = [...originalArray];
shallowCopiedArray.push(4);
console.log(originalArray); // [1, 2, 3]
console.log(shallowCopiedArray); // [1, 2, 3, 4]
:::
split() Method: This method divides a string into an array of substrings and specifies where each split should happen based on a given separator. If no separator is provided, the method returns an array containing the original string as a single element.:::interactive_editor
const str = "hello";
const charArray = str.split("");
console.log(charArray); // ["h", "e", "l", "l", "o"]
:::
reverse() Method: This method reverses an array in place.:::interactive_editor
const desserts = ["cake", "cookies", "pie"];
console.log(desserts.reverse()); // ["pie", "cookies", "cake"]
:::
join() Method: This method concatenates all the elements of an array into a single string, with each element separated by a specified separator. If no separator is provided, or an empty string ("") is used, the elements will be joined without any separator.:::interactive_editor
const reversedArray = ["o", "l", "l", "e", "h"];
const reversedString = reversedArray.join("");
console.log(reversedString); // "olleh"
:::
Review the JavaScript Arrays topics and concepts.