3

what does it mean for the following line?

T = ($("#a .b").hasClass("active") ? "C" : "D") ;

$("#a .b").hasClass("active") means whether #a .b exists? but how about ? "C" : "D", is it some kind of comparison logic?

royhowie
  • 11,075
  • 14
  • 50
  • 67
user2381130
  • 193
  • 2
  • 12

2 Answers2

4

It's a ternary operator

condition ? expr1 : expr2 

If condition is true then expr1 would return else expr2 will return.

So, in your case:

T = ($("#a .b").hasClass("active") ? "C" : "D") ;

T variable will hold "C" if $("#a .b") has class active else it would hold "D"

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
1

It tell you that :

if ( $("#a .b").hasClass("active") ) {    
   T = "C";
} else {
   T = "D"
}

Here you can read this doc for further understanding. Ternary Operator

Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40