+ 1

How the answer is '#10' ?Pls explain the both ternary operator

var a='#'; var b=null; var c; var a+=b==c? '1' : '0' ; var a+=b===c? '1' : '0' ; console.log(a);

30th Jan 2023, 4:24 PM
Mohammad Tausiful Alam
Mohammad Tausiful Alam - avatar
4 ответов
+ 4
Mohammad Tausiful Alam null == undefined // true So b == c //true So a = a + 1 = #1 And b === c // false because null === undefined is false because type is not same So a = a + '0' = #1 + '0' = #10
30th Jan 2023, 4:39 PM
A͢J
A͢J - avatar
+ 4
The ternary operator is basically a short form for a simple "if/else". To rewrite the line: If (b == c) a += '1' Else a += '0'
30th Jan 2023, 4:31 PM
Ausgrindtube
Ausgrindtube - avatar
+ 4
Code produce errors. Line 4,5 are has syntax errors. Check in playground... so it is not a clear question. edit : Mohammad Tausiful Alam it may be var a='#'; var b=null; var c = a+=b==c? '1' : '0' ; // a+=b===c? '1' : '0' ; // console.log(a); then : var a='#'; var b = null; var c = a+=b==c? '1' : '0' ; // it will be evaluated as // a = a + ( b == c? '1' : '0' ) ; so b==c is // null== undefined is true because both // are 0 in int value so a = # + 1 =#1 a+=b===c? '1' : '0' ; // here this again b===c is //false, same value but not same //types so return false hence '0' is added to a => a = #10 console.log(a); // #10
30th Jan 2023, 4:33 PM
Jayakrishna 🇮🇳
+ 1
Thanks to all. Got it
30th Jan 2023, 4:57 PM
Mohammad Tausiful Alam
Mohammad Tausiful Alam - avatar