Jump to content

Common JavaScript Manual/Condition operator

75% developed
From Wikibooks, open books for an open world

Conditional operator

[edit | edit source]

If we need just set value for any variable in depent of condition, we can use conditional operator. It's shorter than "if" expression many times. Let's see example.

name = "Artiom";
cond = true;

//If expression
if(cond){
  name = "Tom";
}else{
  name = "Artem";
}

//Conditional operator
name = cond?"Tom":"Artem";

Nested conditional operator

[edit | edit source]

As 'if' expression so and Conditional operator can be multiple.

name = "Artiom";
cond1 = false;
cond2 = true;

//Conditional operator
name = cond1?"Tom":cond2?"Artem":"NoName"


Logic and Comparison operators · Switch expression