JavaScriptには基本的な型があります。
型というのは、種類のようなものです。
typeof 演算子(オペレーター)を使います。
const num = 123; console.log(typeof num); // number const string = "123"; console.log(typeof string); // string const bool = true; console.log(typeof bool); // boolean
const str = "文字列" const str2 = "文字列2" const str3 = "123" + "456"; console.log(str, str2, str3); // 文字列 文字列2 123456
const num1 = 123; const num2 = 234 + 555; const num3 = 12 * 12; console.log(num1, num2, num3); // 123 789 144
const bool1 = true; const bool2 = false; const bool3 = true && false; const bool4 = true || false; console.log(bool1, bool2, bool3, bool4); // true false false true
日本語にすると未定義です。
文字通り、定義されていない値です。
nullと違うのはnullは「何もない」(何も入れていない)という意味合いで使いますが、undefinedは「定義されていない」といったニュアンスで使われます。
const v = undefined; console.log(v); // undefined const v2 = {a: 1} console.log(v2.b); // undefined
nullは何も値を入れていないという意味合いで使います。
「何も値を値を変数に代入していない」という場合には、undefinedよりもnullを使う傾向にあります。
const a = null; console.log(a); // null