Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Array Methods and Manipulation | Variables and Data Types in JavaScript
/
Introduction to JavaScript

bookArray Methods and Manipulation

メニューを表示するにはスワイプしてください

Arrays are versatile for storing and retrieving data. Retrieving data using square brackets [] is called indexing.

let arr = [1, 2, 3, 4, 5, 6];
arr[3] // This is indexing

However, there are various methods available for working with arrays.

Adding Elements

There are different ways to add elements to an array.

Push

The push() method adds a new value to the end of the array:

1234567
let arr = [1, 2, 3]; arr.push(4); arr.push(5); arr.push(6); console.log(arr);
copy

Unshift

The unshift() method works like the push() method, but it inserts the value at the beginning of the array.

123456
let arr = [1, 2, 3]; console.log("Array:", arr); arr.unshift(222); // Insert element at the start console.log("Array:", arr);
copy

Indexing

You can append a new value by indexing:

123456
let arr = [1, 2]; arr[2] = 3; arr[3] = 4; console.log(arr);
copy

Indexing allows you to assign a value to a specified index, reassign a previous value, and more:

12345
let arr = [1, 2, 3]; arr[0] = 4; console.log("Array:", arr);
copy

To create a new element in the array without mistakes, you can use the push(value) method or the arr[arr.length] = value expression:

12345678910111213
let myArray = []; myArray[myArray.length] = "indexing"; console.log("After first indexing:", myArray); myArray.push("pushing"); console.log("After first pushing:", myArray); myArray[myArray.length] = "indexing"; console.log("After second indexing:", myArray); myArray.push("pushing"); console.log("After second pushing:", myArray);
copy

Deleting Elements

Sometimes, you may need to delete elements from an array. This can be done in different ways.

Pop

The pop() method deletes the last element in an array and allows you to save it to another variable:

1234567
let arr = [11, 22, 33, 44]; console.log("Array:", arr); let x = arr.pop(); // Remove and save the last element in `arr` to variable `x` console.log("Popped element:", x); console.log("Array now:", arr);
copy

Shift

The shift() method works like pop(), but it removes the first element from an array:

123456789101112
let arr = [11, 22, 33, 44, 55, 66]; console.log("Array:", arr); let popped = arr.pop(); // Remove the last element console.log("Popped:", popped); console.log("Array:", arr); let shifted = arr.shift(); // Remove the first element console.log("Shifted:", shifted); console.log("Array:", arr);
copy

1. How can we add an element to the end of the array?

2. How can we remove the last element in the array?

question mark

How can we add an element to the end of the array?

正しい答えを選んでください

question mark

How can we remove the last element in the array?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  7

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 2.  7
some-alt