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

javascript If-Else Statement skip If condition - Stack Overflow

programmeradmin1浏览0评论

As you can see in the picture the value of the r.SalaryGrade is null. Why it skip my IF condition and goes to Else?. I make sure that if is an empty string and null it will add a class to a textbox. Did I do something wrong?.

 if (r.SalaryGrade == "" && r.SalaryGrade == null) { $("#JobGradeId").addClass('validation'); }
 else { $("#JobGradeId").removeClass('validation'); }

As you can see in the picture the value of the r.SalaryGrade is null. Why it skip my IF condition and goes to Else?. I make sure that if is an empty string and null it will add a class to a textbox. Did I do something wrong?.

 if (r.SalaryGrade == "" && r.SalaryGrade == null) { $("#JobGradeId").addClass('validation'); }
 else { $("#JobGradeId").removeClass('validation'); }

Share Improve this question asked Jul 4, 2017 at 1:22 KiRaKiRa 9243 gold badges19 silver badges46 bronze badges 4
  • 1 I'm pretty sure in Javascript '' != null, in other words that if statement will never be entered as you want it to be an empty string and null. – Chris Commented Jul 4, 2017 at 1:24
  • 1 Maybe swap and- && for or- || ? – NewToJS Commented Jul 4, 2017 at 1:25
  • 2 if (r.SalaryGrade == "" || r.SalaryGrade == null) – cs95 Commented Jul 4, 2017 at 1:26
  • Read it out loud. Grade equal to empty string AND Grade equal to null. – epascarello Commented Jul 4, 2017 at 1:35
Add a ment  | 

3 Answers 3

Reset to default 2

Your if condition has an "&&" in it.

'' and null are different things

You need to change how you structure that if statement. You could do something like,

r.SalaryGrade == "" || r.SalaryGrade == null

r.salaryGrade = "" as the first conditional passed, therefore it cannot be equal to null ( as it HAS a value of "" which is not null ).

You can change the && to an || and if either is true it will continue on.

As there is && in the if statement. it does not go in the if statement. Just try this for clarification- just make an if statement

String s= "";

IF(s == null) print "it is null";

else print "it is not null";

See yourself whether String s can be empty and null at the same time.

发布评论

评论列表(0)

  1. 暂无评论