Cyberzinga Cyberzinga - 7 months ago 10
Javascript Question

Logical AND operation without using double ampersands

currently I am using a javascript in my wordpress site. Which will create a div first then will render some content from external source in it. But, it is not working because of && , escaping issue. I need to bypass && . I also tried woth & but it is not working too. After deleting &&, div has been created but not working, as it is functional and I have to perform a AND operation. Is there any other way that I can perform logical AND operation without using && ? Code format :-

if(k.className == "divbox" && k.getAttribute("data-push") != "1") {
JSCode
}

Answer

Disclaimer:

Please do not use this proposal in any production code!
This is for eduactional use only!

You can replace the logical AND && in a condition with a multiplication operator *, because a boolean value becomes either 0 (false) or 1 (true).

The right replacement for it in conditions renders the multiplication to the wanted result.

a  b a*b   result
-  -  -   --------
0  0  0 -> false
0  1  0 -> false
1  0  0 -> false
1  1  1 -> true

Suggestion:

if ((k.className == "divbox") * (k.getAttribute("data-push") != "1")) {