0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaScript基本まとめ(制御構文)

Last updated at Posted at 2025-02-11

制御構文

文法が頭に入っていない時の覚書にご利用ください。だいたいの構文の覚書になっています。各解説はまたいずれまとめたいと思います

  • 条件分岐
// if文
if (条件) {
    // 条件が true の時に実行
} else if (別の条件) {
    // 最初の条件が false で、別の条件が true の時に実行
} else {
    // どの条件も false の時に実行
}

// switch文
switch () {
    case 値1:
        // 値が値1の時に実行
        break;
    case 値2:
        // 値が値2の時に実行
        break;
    default:
        // どのcaseにも当てはまらない時に実行
}

ループ

// for文
for (let i = 0; i < 5; i++) {
    // 5回繰り返し実行
}

// while文
while (条件) {
    // 条件が true の間、繰り返し実行
}

// do-while文
do {
    // 最低1回は実行され、その後条件が true の間繰り返し実行
} while (条件);

// for...of文(配列やイテラブルオブジェクトの要素に対して繰り返し)
const array = [1, 2, 3];
for (const element of array) {
    console.log(element);
}

// for...in文(オブジェクトのプロパティに対して繰り返し)
const obj = { a: 1, b: 2 };
for (const key in obj) {
    console.log(key, obj[key]);
}

関数宣言

// 通常の関数宣言
function greet(name) {
    return `Hello, ${name}!`;
}

// アロー関数
const greetArrow = (name) => {
    return `Hello, ${name}!`;
};

// 省略記法(1行の場合)
const greetShort = name => `Hello, ${name}!`;

コールバック関数

function doSomething(callback) {
    // 処理
    callback();
}

// 使用例
doSomething(() => {
    console.log("コールバック実行");
});

オブジェクト

// オブジェクトの作成
const person = {
    name: "山田太郎",
    age: 30,
    greet() {
        return `私は${this.name}です`;
    }
};

// 通常のプロパティアクセス
console.log(person.name);      // "山田太郎"
console.log(person["name"]);   // "山田太郎"

// 分割代入
const { name, age } = person;  // オブジェクトのプロパティを個別の変数として取り出す
console.log(name);  // "山田太郎"
console.log(age);   // 30

// 分割代入で別名をつける場合
const { name: fullName, age: personAge } = person;
console.log(fullName);   // "山田太郎"
console.log(personAge);  // 30

// 配列の分割代入
const numbers = [1, 2, 3];
const [first, second] = numbers;
console.log(first);   // 1
console.log(second);  // 2

// 関数の引数での分割代入
function printUserInfo({ name, age }) {
    console.log(`${name}さんは${age}歳です`);
}
printUserInfo(person);  // "山田太郎さんは30歳です"

配列

// 配列の作成
const fruits = ["りんご", "バナナ", "オレンジ"];

// 配列操作
fruits.push("ぶどう");     // 末尾に追加
fruits.pop();              // 末尾から削除
fruits.unshift("いちご");  // 先頭に追加
fruits.shift();           // 先頭から削除

// 配列のメソッド
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(num => num * 2);    // 各要素を2倍
const evenNumbers = numbers.filter(num => num % 2 === 0);  // 偶数のみ抽出
const hasThree = numbers.includes(3);  // 要素の存在確認: true
const firstBigNumber = numbers.find(num => num > 3);  // 最初の該当要素: 4

例外処理

try {
    // エラーが発生する可能性のあるコード
    throw new Error("エラーが発生しました");
} catch (error) {
    // エラー処理
    console.error(error.message);
} finally {
    // 必ず実行される処理
}
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?