+ 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);
4 Respostas
+ 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
+ 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'
+ 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
+ 1
Thanks to all. Got it