JS Coding Questions Logo
JS Coding Questions
#175💼 Interview💻 Code

Can you apply chaining on conditional operator

Advertisement

728x90

Yes, you can apply chaining on conditional operators similar to if … else if … else if … else chain. The syntax is going to be as below,

javascript
1function traceValue(someParam) {
2  return condition1
3  ? value1
4  : condition2
5  ? value2
6  : condition3
7  ? value3
8  : value4;
9  }
10
11  // The above conditional operator is equivalent to:
12
13function traceValue(someParam) {
14  if (condition1) {
15  return value1;
16  } else if (condition2) {
17  return value2;
18  } else if (condition3) {
19  return value3;
20  } else {
21  return value4;
22  }
23}

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 4

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
175of476