JavaScript における null と undefined の違いは何ですか? 質問する

JavaScript における null と undefined の違いは何ですか? 質問する

JavaScript のnullの違いは何ですか?undefined

ベストアンサー1

undefined変数は宣言されているが、まだ値が割り当てられていないことを意味します。

var testVar;
console.log(testVar); //shows undefined
console.log(typeof testVar); //shows undefined

nullは代入値です。値なしの表現として変数に割り当てることができます。

var testVar = null;
console.log(testVar); //shows null
console.log(typeof testVar); //shows object

前述の例から、undefinedと はnull2 つの異なる型であることがわかります。undefinedは型自体 (未定義) ですが、nullはオブジェクトです。

証拠 :

console.log(null === undefined) // false (not the same type)
console.log(null == undefined) // true (but the "same value")
console.log(null === null) // true (both type and value are the same)

そして

null = 'value' // Uncaught SyntaxError: invalid assignment left-hand side
undefined = 'value' // 'value'

おすすめ記事