javascript If-Else Statement skip If condition - Stack Overflow

As you can see in the picture the value of the r.SalaryGrade is null. Why it skip my IF condition and g

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.

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

相关推荐

  • javascript If-Else Statement skip If condition - Stack Overflow

    As you can see in the picture the value of the r.SalaryGrade is null. Why it skip my IF condition and g

    20小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信