admin管理员组

文章数量:1425161

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');
}

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');
}
Share Improve this question asked Jan 15, 2013 at 22:47 RajeevRajeev 1,4237 gold badges31 silver badges48 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

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 pare them directly with true.

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

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');
}

本文标签: javascript true and true returning false in if statementStack Overflow