简体   繁体   中英

javascript true and true returning false in if statement?

In the plain javascript function. Both the values, min_chk and max_chk are true, but the if function still shows the alert. not able to figure why?

function Checkit(m,n){
return m>n;
}

var min_chk = Checkit(a,X);
var max_chk = Checkit(b,Y);
if ((min_chk === 'true') && (max_chk === 'true')){
...
    } else {
    alert('invalid range');
}

The boolean true is not the same as the string 'true' . Remove the quotes.

The === operator returns false if the operands on both sides have different types. The boolean true and the string "true" have different types.

You should change your check just to

if (min_chk && max_chk)

Since min_chk and max_chk are already booleans, you don't need to compare them directly with true .

Get rid of the '' around true

function Checkit(m, n) {
    return m > n;
}

var min_chk = Checkit(a, X);
var max_chk = Checkit(b, Y);
if ((min_chk === true) && (max_chk === true)) {...
} else {
    alert('invalid range');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM