厳密比較演算子が必要な場面

つまり、比較演算だと同じものとされるものの組み合わせ。

  • null==undefined
  • ==false、==0、[]==""
  • false==[]、false==0、false==""、false=="0"
  • 0==[]、0==false、0==""、0=="0"
  • ""==[]、""==false、""==0
  • "0"==false、"0"==0

空のオブジェクトは含まれないけど、空の配列は含まれることに注意。

あと、!!でfalseになるもの。

  • null
  • undefined
  • false
  • 0
  • ""
var literal = {
	'null': null,
	'undefined': void(0),
	'{}': {},
	'[]': [],
	'false': false,
	'true': true,
	'0': 0,
	'1': 1,
	'""': "",
	'"0"': "0",
	'"1"': "1"
};

document.write('<table><tbody>');

document.write('<tr>');
document.write('<th>==</th>');
for (var i in literal)
	document.write('<th>'+i+'</th>');
document.write('</tr>');

for (var i in literal) {
	document.write('<tr>');
	document.write('<th>'+i+'</th>');
	for (var j in literal)
		document.write('<td>'+(literal[i]==literal[j])+'</td>');
	document.write('</tr>');
}

document.write('</tbody></table>');