LVLPATH

日本最強無料プログラミングスクール

PATHSERVICEHANDS ONMANZI
JavaScript配列

JavaScript基礎文法: 配列

✅ 配列とは

JavaScript における 配列(Array) は、複数の値を 1 つの変数にまとめて管理できるデータ構造 です。

たとえば「りんご・バナナ・みかん」のように、関連するデータをまとめて扱いたいときに便利です。

🔸 特徴

順序を持ったデータの集まりです。先頭はインデックス 0 から始まります。

異なる型のデータ(数値・文字列・オブジェクト・関数 など)を混ぜて入れることができます。

配列の中に配列を入れる「多次元配列」のような使い方もできます。

🔸 例(文字列の配列)

const fruits = ["apple", "banana", "orange"];

fruits[0] → "apple"

fruits[1] → "banana"

fruits[2] → "orange"

✅ 基本構文

const 配列名 = [値1, 値2, 値3];

✅ 例

const fruits = ["apple", "banana", "orange"];
const numbers = [1, 2, 3, 4, 5];
const mixed = [true, 123, "hello", { name: "Taro" }];

✅ 練習

コピペではなく実際に打ち込んで練習してみましょう。

配列の要素にアクセス

const fruits = ["apple", "banana", "orange"];
console.log(fruits[0]); // "apple"
console.log(fruits[2]); // "orange"

配列の長さ(要素数)を取得

const fruits = ["apple", "banana", "orange"];
console.log(fruits.length); // 3

配列の末尾に追加(push)

const fruits = ["apple", "banana", "orange"];
fruits.push("grape");
console.log(fruits);
// ["apple", "banana", "orange", "grape"]

配列の末尾を削除(pop)

const fruits = ["apple", "banana", "orange"];
fruits.pop();
console.log(fruits);
// ["apple", "banana", "orange"]

配列の先頭に追加(unshift)

const fruits = ["apple", "banana", "orange"];
fruits.unshift("melon");
console.log(fruits);
// ["melon", "apple", "banana", "orange"]

配列の先頭を削除(shift)

const fruits = ["apple", "banana", "orange"];
fruits.shift();
console.log(fruits);
// ["apple", "banana", "orange"]

配列の一部を取り出す(slice)

const fruits = ["apple", "banana", "orange"];
const newFruits = fruits.slice(1, 3);
console.log(newFruits);
// ["banana", "orange"]

配列をループする(forEach)

const fruits = ["apple", "banana", "orange"];
fruits.forEach(fruit => {
  console.log(fruit);
});
// apple
// banana
// orange

✅ まとめ(配列)

  • 複数の値をまとめて扱えるのが配列
  • push や pop で要素を追加・削除
  • forEach, map, filter, reduce などの高階関数(関数型処理)と組み合わせるとより便利
お問い合わせ