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"

25th Mar 2020, 5:31 AM
Mayank Dhillon
Mayank Dhillon - avatar
4 ответов
+ 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
25th Mar 2020, 5:52 AM
Farhan
Farhan - avatar
+ 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'
25th Mar 2020, 5:45 AM
A͢J
A͢J - avatar
+ 2
Farhan Yes I got because here a = 5 not a == 5. Thanks
25th Mar 2020, 6:01 AM
A͢J
A͢J - avatar
+ 1
The problem was the condition itself. Instead of '=' I was supposed to use '==' after which the code worked as intended. Thanks for answering.
25th Mar 2020, 6:04 AM
Mayank Dhillon
Mayank Dhillon - avatar