Why is there no comparison in the javascript instruction "If ... Else ..."?

In the following code

var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

part of '$ active.next (). length 'doesn't seem to compare anything, and I don't understand how a condition is defined as True or False.

Or he says that: if the different $ next are equal to $ active.next (). length, then the condition is true?

+5
source share
5 answers

In javascript, any expression can be converted to a true or false value and, therefore, it is valid in the place of comparison. Values ​​that are false in javascript,

  • false
  • 0
  • "" (empty line)
  • null
  • undefined
  • NaN

In this case, it lengthrefers to a numerical value, and if it is evaluated as 0, then it will be considered false. Otherwise it will be true.

+9

length 0 undefined (.. $active ), false.

+5

$active.next().length - true, , , $next = $active.next(). $next = $('#slideshow IMG:first'). ? . if else.

+2

, :

if($active.next().length) {
    $next = $active.next();
}
else {
    $next = $('#slideshow IMG:first');
}

, $active.next(). length, 0 . , , JavaScript true, false false.

+1

, , If... Else... .

, :

if($active.next().length){
 $next = $active.next();
}else {
 $next = $('#slideshow IMG:first');
}
0
source

All Articles