javascript - Can a variable be produced from an if else statement? -


can change variables value based on if/else statement in javascript?

var $nextlink = $this.next().attr('href'),  $currentlink = $this.attr('href');  if ($currentlink == $nextlink){              // check if next link same current link   var $nextload = $this.eq(2).attr('href');  // if so, next link after next } else {var $nextload = $nextlink;} 

the code shown in question work. note, though, javascript doesn't have block scope, function scope. is, variables declared inside if or else statement's {} block (or for statement's {}, etc.) visible in surrounding function. in case think that's intend, still js coders find neater declare variable before if/else , set value if/else.

neater still in 1 line using ?: conditional (or ternary) operator:

var $nextload = $currentlink == $nextlink ? $this.eq(2).attr('href') : $nextlink; 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -