Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i make this javascript logic + syntax (very simple)

Tags:

javascript

Why I am getting true all the time?

var _template_id = "";
if (_template_id != '0' || _template_id!="")  {
        alert("true")
}else{
    alert("false")
}

or even if i set _template_id="0", it still turns out as true...

like image 513
tv4free Avatar asked Aug 30 '26 17:08

tv4free


2 Answers

Because you're asking if _template_id isn't equal to "0" OR isn't equal to "". One of those will always be true.

Proof: Given your statement of (_template_id != '0' || _template_id!=""), let's suppose that the first part is false. Therefore _template_id == '0' is true, and hence _template_id != "" is true, so overall the statement evaluates to true.

If, on the other hand, the first part is true, then clearly the whole thing evaluates to true again.

Therefore, the statement is always true.

like image 158
Ben Jackson Avatar answered Sep 01 '26 06:09

Ben Jackson


You want && not ||.

It must always be true because "0" != "", so one or the other is always true.

like image 36
Jason Nichols Avatar answered Sep 01 '26 05:09

Jason Nichols



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!