javascript – What is the behavior of "value?'': Value"?

Question: Question:

What is the behavior of the following parts?

function toggle(element,classe) {
    element.className = element.className ? '' : classe; //この部分です。 
}

Answer: Answer:

It's aternary operator .

Conditional expression? Value when the condition is true: Value when the condition is false

When the condition is true, the value of the whole expression is "value when the condition is true", and when it is false, it is "value when the condition is false".

that's why,

element.className = element.className ? '' : classe;

teeth,
If a class name is set for the element ( element.className ), make it an empty string ( '' ).
Otherwise (that is, if the class name is not set) set classe as the class name.
It means that · · ·

Scroll to Top