Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Adding Values to an Array | Section
JavaScript Fundamentals

bookAdding Values to an Array

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

After defining an array, we can further add some elements to it using the push method.

Note
Definition

A methods is a function that belongs to an object and is called using that object. The main difference is that a function is independent, while a method is tied to an object and is called using object.methodName(). For example, in this case the object is the array and the method is push.

The general syntax of the push method is:

arrayName.push(value1, value2, …);

Here arrayName is the name of the array to which we want to add the element, and value1, value2 and so on are the elements we want to add to that array.

Pushing an element to an array adds it to the end of the array:

12345
let fruits = ["Apple", "Banana"]; console.log(fruits); // Output: ["Apple", "Banana"] fruits.push("Cherry"); console.log(fruits); // Output: ["Apple", "Banana", "Cherry"]
copy

Notice how "Cherry" was added to the end of the fruits array using the push method.

If we have multiple values to push, they are pushed in the same order they were mentioned:

12345
let fruits = ["Apple", "Banana"]; console.log(fruits); // Output: ["Apple", "Banana"] fruits.push("Cherry", "Mango"); console.log(fruits); // Output: ["Apple", "Banana", "Cherry", "Mango"]
copy

1. What does the push method do in JavaScript?

2. Which of the following is the correct syntax for using the push method?

3. What will be the output of the following code?

question mark

What does the push method do in JavaScript?

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

question mark

Which of the following is the correct syntax for using the push method?

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

question mark

What will be the output of the following code?

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

すべて明確でしたか?

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

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

セクション 1.  57

AIに質問する

expand

AIに質問する

ChatGPT

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

セクション 1.  57
some-alt