JS 6th edition reading circle part 210. null と undefined
● null
○ 「値がない」ことを示す
○ プログラムレベルで予定どおりの場合を表すもの
● undefined
○ 「値がない」ことを示す
○ システムレベルで予期せぬ、エラーのような場合に表す
もの
11. undefined (1 / 2)
var hoge = ‘test’;
console.log(hoge); // test
var piyo;
console.log(piyo); // undefined
12. undefined (2 / 2)
var executeA = function() { return 1 + 2; };
console.log(executeA()); // 3
var executeB = function() { 1 + 2; };
console.log(executeB()); // undefined
13. イメージ
var hoge = ‘test’;
‘test’
var hoge = null;
null
var hoge; // undefined
15. hoge は文字列なはずなのに・・・?
var hoge = ‘256’;
console.log(hoge / 16); // 16
if (hoge) { console.log(‘Hello!’); } // Hello!
console.log(hoge.indexOf(‘56’)); // 1
18. ラッパーオブジェクト (1 / 2)
var s = new String(‘ABC’);
console.log(s.toLocaleLowerCase()); // abc
var n = new Number(12300000);
console.log(n.toExponential()); // 1.23e+7
var b = new Boolean(false);
if (b) { console.log(b); } // true
19. ラッパーオブジェクト (2 / 2)
console.log(
‘ABC’.toLocaleLowerCase()); // abc
console.log(
12300000.toExponential()); // 1.23e+7
25. オブジェクトから文字列に変換 (2 / 2)
var hoge = {
toString: function() {
return ‘toString’;
},
valueOf: function() {
return ‘valueOf’;
}
};
console.log(hoge); // toString
27. オブジェクトから文字列に変換 (2 / 3)
var hoge = {
toString: function() {
return 1;
},
valueOf: function() {
return 2;
}
};
console.log(hoge - 2); // 0
28. オブジェクトから文字列に変換 (3 / 3)
var hoge = {
toString: function() {
return 1;
},
valueOf: function() {
return { a: 2 };
}
};
console.log(hoge - 2); // -1