0
0

More than 3 years have passed since last update.

Javascript 配列

Last updated at Posted at 2021-05-31

本日は配列について学習しましたので、そちらについて記事に残していきます。

配列を使わずに複数の値を扱う際は、下記のように何行にも分けて書くことになりますので見づらくなりがちです。

let x = "キリン";
let y = "イルカ";
let z = "";

配列を使って複数の値を扱う際は、[]の中に値を格納するので何が格納されているか分かりやすくなると思います。

let animal = ["キリン", "イルカ", ""];

配列の中身を出力したければ下記のようにすれば出力が可能です。

let animal = ["キリン", "イルカ", ""];
console.log(animal); // ["キリン", "イルカ", "猫"]

また、配列を作ると自動的に値に index と呼ばれる番号が割り当てられるのですが、こちらは0から始まる番号が割り当てられるので注意が必要です。

let animal = ["キリン", "イルカ", ""]; //"キリン"は[0]、"イルカ"は[1]、"猫"は[2]
let kirin = animal[0];
let iruka = animal[1];
let neko = animal[2];

console.log(kirin); // キリン
console.log(iruka); // イルカ
console.log(neko); // neko

このように配列の何番目という形で指定することによって、配列の任意の番号の値を出力することが可能です。

配列の中身の数を知りたい際は、変数名.lengthという形で呼び出すことによって中身の数を出力することが可能です。

let animal = ["キリン", "イルカ", ""];
console.log(animal.length); // 3
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0