Array Methods Part 3
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 68 of 77.
Here are a few more useful array methods:
1. concat(): Combines two or more arrays and returns a new array, without modifying the original arrays.
let arr1 = [1, 2];
let arr2 = [3, 4];
let combined = arr1.concat(arr2);
console.log(combined); // Output: [1, 2, 3, 4]2. join(separator): Joins all elements of an array into a string, separated by the specified separator (optional).
let fruits = ["apple", "banana", "orange"];
let joined = fruits.join(", ");
console.log(joined); // Output: "apple, banana, orange"3. slice(start, end): Extracts a section of an array and returns a new array. The start index is inclusive, and the end index is exclusive.
let numbers = [1, 2, 3, 4, 5];
let sliced = numbers.slice(1, 4);
console.log(sliced); // Output: [2, 3, 4]4. splice(start, deleteCount, item1, item2, ...): Changes the contents of an array by removing, replacing, or adding elements. It modifies the original array and returns an array containing the deleted elements (if any).
let numbers = [1, 2, 3, 4, 5];
let removed = numbers.splice(2, 2, 6, 7);
console.log(numbers); // Output: [1, 2, 6, 7, 5]
console.log(removed); // Output: [3, 4]5. split(separator, limit): Splits a string into an array of substrings based on the specified separator. The original string is not modified. The optional limit parameter controls the maximum number of splits.
let text = "apple,banana,orange";
let result = text.split(",");
console.log(result);
// Output: ["apple", "banana", "orange"]
let limited = text.split(",", 2);
console.log(limited);
// Output: ["apple", "banana"]Challenge
EasyYou are given an array numbers = [1, 2, 3, 4, 5]. Perform the following steps and print the results directly:
- Use
concat()to add[6, 7, 8]to the end of the array and print the result. - Use
join()to convert the result ofconcat()into a string separated by commas and print it. - Use
slice()to extract the first three elements from the originalnumbersarray and print them. - Use
splice()to replace the second element ofnumberswith99and print the modifiednumbersarray.
Cheat sheet
Array methods for manipulation and extraction:
concat(): Combines arrays without modifying originals
let arr1 = [1, 2];
let arr2 = [3, 4];
let combined = arr1.concat(arr2);
console.log(combined); // Output: [1, 2, 3, 4]join(separator): Joins array elements into a string
let fruits = ["apple", "banana", "orange"];
let joined = fruits.join(", ");
console.log(joined); // Output: "apple, banana, orange"slice(start, end): Extracts section of array (start inclusive, end exclusive)
let numbers = [1, 2, 3, 4, 5];
let sliced = numbers.slice(1, 4);
console.log(sliced); // Output: [2, 3, 4]splice(start, deleteCount, item1, item2, ...): Modifies original array by removing/replacing/adding elements
let numbers = [1, 2, 3, 4, 5];
let removed = numbers.splice(2, 2, 6, 7);
console.log(numbers); // Output: [1, 2, 6, 7, 5]
console.log(removed); // Output: [3, 4]split(separator, limit): Splits a string into an array of substrings
let text = "apple,banana,orange";
let result = text.split(",");
console.log(result); // Output: ["apple", "banana", "orange"]
let limited = text.split(",", 2);
console.log(limited); // Output: ["apple", "banana"]Try it yourself
let numbers = [1, 2, 3, 4, 5];This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionFunction ExpressionDefault ParametersArrow FunctionsRecap - Validation Function12Arrays Part 2
Iterating Over ArraysThe forEach Methodfor...of LoopRecap - P CounterArray SlicingArray Methods Part 3Array Methods Part 4Membership Testing