How to negate code in "if" statement block in JavaScript -JQuery like 'if not then..'

JavascriptJqueryIf StatementConditional Statements

Javascript Problem Overview


For example if I want to do something if parent element for used element hasn't got ul tag as next element, how can I achieve this?

I try some combination of .not() and/or .is() with no success.

What's the best method for negate code of a if else block?

My Code

if ($(this).parent().next().is('ul')){
   // code...
}

I want to achieve this

Pseudo Code:

if ($(this).parent().next().is NOT ('ul')) {
    //Do this..
}

Javascript Solutions


Solution 1 - Javascript

You can use the Logical NOT ! operator:

if (!$(this).parent().next().is('ul')){

Or equivalently (see comments below):

if (! ($(this).parent().next().is('ul'))){

For more information, see the Logical Operators section of the MDN docs.

Solution 2 - Javascript

Try negation operator ! before $(this):

if (!$(this).parent().next().is('ul')){

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkrzyhubView Question on Stackoverflow
Solution 1 - JavascriptJustin EthierView Answer on Stackoverflow
Solution 2 - JavascriptChanduView Answer on Stackoverflow