[Solved] Simple javascript if statement with charAt is not working


You need to invert the logic conditions as both states cannot possibly be true at the same time, so x is always set to false. Try this:

var chr = $(this).val().charAt(0);
if (chr == '6' || chr == '9') {
    x = true;
} else {
    x = false;
}

From there you can now see that you don’t even need the if condition as you can set x directly, like this:

var chr = $(this).val().charAt(0);
var x = chr == '6' || chr == '9';

solved Simple javascript if statement with charAt is not working