Hatena::Groupmnemonic

きおく屋めがねばし

 | 

2012-11-26

「3.4 null と undefined」

null and undefined

null is a language keyword that evaluates to a special value that is usually used to indicate the absence of a value. Using the typeof operator on null returns the string "object", indicating that null can be thought of as a special object value that indicates "no object". In practice, however, null is typically regarded as the sole member of its own type, and it can be used to indicate "no value" for numbers and strings as well as objects.

JavaScript also has a second value that indicates absence of value. The undefined value represents a deeper kind of absence.

初期化されていない変数の値や、存在しないオブジェクトプロパティや配列の要素を読み出したときの値が未定義値です。戻り値のない関数が返す値も未定義値です。関数の呼び出し時に値が指定されなかった、引数の値も未定義値です。

未定義値に対して typeof 演算子を使うと、未定義値を表す "undefined" が返されます。

Despite these differences, null and undefined both indicate an absence of value and can often used interchangeably. The equality operator == considers them to be equal. (Use the strict equality operator === to distinguish them.)

両者とも false に変換される値です。

nullundefined もプロパティやメソッドを持っていません。実際に . や [] を使ってプロパティやメソッドにアクセスしようとすると TypeError が発生します。

You might consider undefined to represent a system-level, unexpected, or error-like absence of value and null to represent program-level, normal, or expected absence of value. If you need to assign one of these values to a variable or property or pass one of these values to a function, null is almost always the right choice.


トラックバック - http://mnemonic.g.hatena.ne.jp/paragramma/20121126
 |