最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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');
}
发布评论

评论列表(0)

  1. 暂无评论