Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.. } 
like image 261
krzyhub Avatar asked Jul 13 '11 18:07

krzyhub


People also ask

How do you stop an if statement?

An IF statement is executed based on the occurrence of a certain condition. IF statements must begin with the keyword IF and terminate with the keyword END.

What can I use instead of if-else in JavaScript?

Javascript offers a ternary operator which acts like a one-line if / else statement with a single condition. It works really well for conditionally assigning a value to a variable.


1 Answers

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.

like image 170
Justin Ethier Avatar answered Sep 23 '22 15:09

Justin Ethier