Noitidart Noitidart - 1 year ago 71
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download