Menu
Coddy logo textTech

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 icon

Challenge

Easy

You are given an array numbers = [1, 2, 3, 4, 5]. Perform the following steps and print the results directly:

  1. Use concat() to add [6, 7, 8] to the end of the array and print the result.
  2. Use join() to convert the result of concat() into a string separated by commas and print it.
  3. Use slice() to extract the first three elements from the original numbers array and print them.
  4. Use splice() to replace the second element of numbers with 99 and print the modified numbers array.

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];
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals