出力
console.log("text"); // 改行ありprocess.stdout.write("text"); // 改行なしconsole.error("text"); // 標準エラー出力console.log(1, "a", { b: 2 }); // 1 a { b: 2 }
分岐 (if)
const v = 1;if (v === 1) {console.log("One");} else {console.log("Other");}
普通のループ (for)
const a = ["a", "b", "c"];for (let i = 0; i < a.length; i++) {console.log(a[i]); // a -> b -> c}
配列の要素でループ (for-of)
const a = ["a", "b", "c"];for (const v of a) {console.log(v); // a -> b -> c}
配列のインデックスでループ (for-in)
const a = ["a", "b", "c"];for (const v in a) {console.log(v); // 0 -> 1 -> 2}
オブジェクトのループ
const o = { a: "あ", b: "ぶ", c: "く" };for (const k of Object.keys(o)) {console.log(k); // a -> b -> c}for (const v of Object.values(o)) {console.log(v); // あ -> ぶ -> く}
基本的な型
const n1: number = 42;const n2: number = 3.1415;const s: string = "text"; // 文字と文字列は同じconst b: boolean = true; // or falseconst a: number[] = [1, 2, 3];const o1: { a: number; b: number } = { a: 1, b: 2 };const o2: { [keys: string]: number } = { "1": 1, あ: 2 };
算術演算
console.log(1 + 2); // 3console.log(3 - 2); // 1console.log(2 * 3); // 6console.log(4 / 2); // 2console.log(5 / 2); // 2.5console.log(7 % 5); // 2console.log(2 ** 3); // 8console.log(2 ** -1); // 0.5
数値計算(小数点以下)
// 切り捨て(n以下の最大の整数。-側)Math.floor(2.1); // 2Math.floor(-2.1); // -3// 切り上げ(n以上の最小の整数。+側)Math.ceil(2.1); // 3Math.ceil(-2.1); // -2// 小数点以下削除Math.trunc(2.1); // 2Math.trunc(-2.1); // -2// 四捨五入Math.round(2.4); // 2Math.round(2.5); // 3Math.round(-2.5); // -2Math.round(-2.6); // -3
数値計算(符号)
// 絶対値Math.abs(-42); // 42// 符号Math.sign(3); // 1Math.sign(0); // 0Math.sign(-3); // -1
数値の大小比較
Math.min(-1, 0, 2); // -1Math.max(-1, 0, 2); // 2
文字列定義
const s1 = 'I\'m single';const s2 = "I'm double";const s3 = `newline`; // == "new\nline"const v = 42;const s4 = `num: ${v}`; // num: 42
空白・改行の削除
const s = "\n aaa \n"s.trim() // "aaa"
ゼロパディング(0埋め)
// 文字列化してから処理するString(12).padStart(4, "0"); // 0012
文字列の出現回数を調べる
const s = "abcaA";// 正規表現直書き版(s.match(/a/g) || []).length; // 2(s.match(/z/g) || []).length; // 0// 変数で調べる場合は RegExp を使うconst c = "a";(s.match(new RegExp(c, "g")) || []).length; // 2(s.match(new RegExp(c, "gi")) || []).length; // 3
配列の初期化
// 空の配列const a: number[] = []; // []a.push(42); // [42]// 長さを指定const b = new Array(3); // [undefined, undefined, undefined]b[0] = 5; // [5, undefined, undefined]b[1] = 7; // [5, 7, undefined]// 初期値で満たすconst c = new Array(3).fill(-1); // [-1, -1, -1]// 初期値で配列やオブジェクトを使うと、全部共有されるconst d = new Array(3).fill([1]); // [[1], [1], [1]]d[0].push(2); // [[1,2], [1,2], [1,2]]
配列の分割代入
const array = [1, 2, 3];const [a, b, c] = array; // 1, 2, 3const [d, e] = array; // 1, 2const [f, ...g] = array; // 1, [2, 3]const [h, , i] = array; // 1, 3
配列の操作
const a = [0, 1];// 末尾に追加 複数指定可a.push(2, 3); // [0, 1, 2, 3]// 末尾から取り除くa.pop()); // 戻り値 3、配列 [0, 1, 2]a.pop()); // 戻り値 2、配列 [0, 1]// 先頭に追加 複数指定可a.unshift(-2, -1); // [-2, -1, 0, 1]// 先頭から取り除くa.shift(); // 戻り値 -2、配列 [-1, 0, 1]a.shift(); // 戻り値 -1、配列 [0, 1]
配列の一部を取得
const a = [0, 1, 2, 3, 4];// 開始から末尾(含まない)までを取得a.slice(0, 2); // [0, 1]a.slice(1, 4); // [1, 2, 3]// 末尾を省略したときは最後までa.slice(2); // [2, 3, 4]
配列の要素を削除して取り出す
// 指定した位置以降の要素を削除するconst a = [0, 1, 2, 3, 4];const b = a.splice(3);console.log(a); // [0, 1, 2]console.log(b); // [3, 4]// 指定位置から指定数の要素を削除するconst c = [0, 1, 2, 3, 4];const d = c.splice(1, 2);console.log(c); // [0, 3, 4]console.log(d); // [1, 2]// 指定位置から指定数の要素を削除して要素を追加するconst e = [0, 1, 2, 3, 4];const f = e.splice(1, 2, 97, 98, 99);console.log(e); // [0, 97, 98, 99, 3, 4]console.log(f); // [1, 2]
配列のソート (number)
const a = [10, 1, 5];// デフォルトでは文字列としてソートa.sort(); // [1, 10, 5]// 昇順ソートa.sort((a, b) => a - b); // [1, 5, 10]// 降順ソートa.sort((a, b) => b - a); // [10, 5, 1]
配列から条件に一致したものを取り出す
const a = [1, 2, 3, 4, 5, 6];const v = a.filter((v) => v % 2 === 0); // [2, 4, 6]const l = a.filter((v) => v % 2 === 0).length; // 3
配列を文字列化
const a = [1, 2, 3];// 何も指定しないときはカンマ区切りa.join(); // 1,2,3// 区切り文字列を指定a.join(" "); // 1 2 3a.join(" - "); // 1 - 2 - 3a.join(""); // 123
重複を許さないSet
// 重複したものは追加されない// 追加された順番を維持するconst s = new Set<number>();s.add(3); // 3s.add(1); // 3, 1s.add(3); // 3, 1s.add(2); // 3, 1, 2console.log(s.size); // 3// Set を Array に変換const a = Array.from(s); // [3, 1, 2]// Array を Set に変換const s2 = new Set([4, 6, 5]); // 4, 6, 5