-3

I was wondering why

recommendation=false;
var val = "Bipin " + recommendation?1:0;
console.log(val);

returns

1

but

recommendation=false;
var x=recommendation?1:0;
var val = "Bipin " + x;
console.log(val);

prints

Bipin 0

Can anyone explain this behavior?

Bipin Bhandari
  • 2,694
  • 23
  • 38

1 Answers1

0
"Bipin " + recommendation?1:0; 

is interpreted as

("Bipin " + recommendation)?1:0

in which case

"Bipin " + recommendation(is always truthfully)

hense the result is always 1

like @Qantas 94 Heavy pointed out this is an operator precedence issue you need to do

 "Bipin " + (recommendation?1:0); 
Dayan Moreno Leon
  • 5,357
  • 2
  • 22
  • 24