0

# Conditional Operator '?'

let a = 10; console.log( (a < 5) ? "a is less than 5" : (a = 5) ? "a is equal to 5" : "a is greater than 5" ); //Why the output is not "a is equal to 5" but not "a is greater than 5"

3/25/2020 5:31:29 AM

Mayank Dhillon

+4

AJ #Level 20 End Game No, output is "a is equal to 5" Rest are correct, but single equal sign(=) assigned a's value to 5, where double equal sign(==) checks whether the operands' values are equal. Copy that and check this in the playground

+3

Look above example like this. x = (a< 5) ? y : ((a = 5) ? z : w); Here is 2 ternary operator so in first ternery operator a < 5 is false so output will be (a = 5) ? z : w //here a = 5 false so output will be w = w = 'a is greater than 5' Edited : here a = 5 not a == 5 so answer will be 'a is equal to 5'

+2

Farhan Yes I got because here a = 5 not a == 5. Thanks

+1

The problem was the condition itself. Instead of '=' I was supposed to use '==' after which the code worked as intended. Thanks for answering.