Noitidart Noitidart - 5 months ago 27
Javascript Question

Why "strict" testing when undefined vs false

I have an object like this:

function doit() {
var current_mods = {
alt: false,
shift: false

var required_mods = {};

for (var modname in current_mods) {
if (current_mods[modname] != required_mods[modname]) {


It keeps telling me mismatch. But I am not doing
, shouldn't
undefined != false
return false, as they are both falsey?


The rules for == and != comparison state that a comparison of two values of different types when one of them is boolean should be done by converting the boolean to a number and then proceeding. Thus your false is converted to 0 and the comparison is treated as if it were

undefined != 0

That, in turn, falls off the end of the comparison process to the default result of false.