0

Guys so I'm new to javascript, I'm trying to do simple things and I found this piece of code for changing divs(which i named k) color when clicking it but I don't really understand what is it I know few things but one thing I can't find is that question mark. Can someone explain what is it and what it does. Thank you

var x = document.getElementById("k"),
    change = false;
    
x.onclick = function() {
  change = !change;
  x.style.background = change? "red": "lime";
}
x.style.background = "lime";

2 Answers2

-2

That is the conditional operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

Steve
  • 5,560
  • 9
  • 13
-2

It is a ternary operator. It's as if you are doing it like this:

if(change===true){
x.style.background = "red"
}else{
x.style.background = "lime"
}
Chhun Socheat
  • 22
  • 1
  • 3