LoginSignup
1
0

More than 3 years have passed since last update.

【JavaScript】undefined と nullの違い【備忘録】

Last updated at Posted at 2021-02-13

undefined

変数の値が定義されていないことを表す値。以下のケースで返却される。

  1. 変数が宣言済みであるが、値を与えられていない時
  2. 未定義のプロパティや配列の要素を参照しようとした時
  3. 関数で値が返されなかった時
// 1
var x;
console.log(x); // undefined

// 2
var obj = { text: "teststring" };
console.log(obj); // undefined

// 3
var y = console.log("test");
console.log(y); // undefined

null

空の状態を表すための値。
関数の返却値として考えると、

  • undefinedは返す値が決まっていない・定義されていない時に未定義の意味で返す
  • nullは何か返すべきだけど、返すものがないため空値を返す

参照

1
0
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
1
0