Sometimes, we want to negate code in an “if” statement block in JavaScript and jQuery.
In this article, we look at how to negate code in an “if” statement block in JavaScript and jQuery.
Negate Code in an “if” Statement Block in JavaScript and jQuery
To negate code in an “if” statement block in JavaScript and jQuery, we can use the logical NOT operator.
For instance, if we want to check if the parent element’s sibling isn’t a ul
element with jQuery, we write:
if (!$(this).parent().next().is('ul')){
//...
}
This is the opposite of:
if ($(this).parent().next().is('ul')){
//...
}
which checks if the parent element’s sibling is a ul
element.
Conclusion
To negate code in an “if” statement block in JavaScript and jQuery, we can use the logical NOT operator.