javascript true and true returning false in if statement? - Stack Overflow

In the plain javascript function. Both the values, min_chk and max_chk are true, but the if function st

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745415947a4626749.html

相关推荐

  • javascript true and true returning false in if statement? - Stack Overflow

    In the plain javascript function. Both the values, min_chk and max_chk are true, but the if function st

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信