null
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
試してみましょう
function getVowels(str) { const m = str.match(/[aeiou]/gi); if (m === null) { return 0; } return m.length;}console.log(getVowels("sky"));// Expected output: 0
構文
js
null;
解説
null
値はnull
というリテラルです。null
はundefined
のようなグローバルオブジェクトのプロパティではありません。代わりに、null
は識別できないことを表し、変数がオブジェクトを指していないことを示します。 API においては、通常はオブジェクトが返されるところで、関連したオブジェクトがない場合にnull
がよく渡されます。
js
// foo が存在せず、定義も初期化もされていない場合:foo; //ReferenceError: foo is not defined
js
// foo が存在しているが、型も値も持たない場合:var foo = null;foo; //null
例
null
とundefined
の違い
null
やundefined
をチェックする際は、等価 (==) と 厳密等価 (===) 演算子の違い に注意してください(前者では型変換が行われます)。
js
typeof null; // "object" (歴史的な理由で "null" ではありません)typeof undefined; // "undefined"null === undefined; // falsenull == undefined; // truenull === null; // truenull == null; // true!null; // trueisNaN(1 + null); // falseisNaN(1 + undefined); // true
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-null-value |