Reputation: 1765
Suppose I want to incorporate a boolean value in some string I print out, like
var x=1;
var y=2;
document.write("<p>The result is "+x==y+"</p>");
Normally, this does not give me the requred output. Is there a method to directly print boolean expressions in the document.write() itself? I do not want to use if-else and then assign separate values to variables and then print them. PS - I have just started learning JavaScript.
Upvotes: 1
Views: 2401
Reputation: 4486
var x=1;
var y=2;
document.write("<p>The result is "+(x==y)+"</p>");
Will do
The result is false
Upvotes: 1
Reputation: 160181
Put parentheses around the boolean expression:
document.write("<p>The result is " + (x == y) + "</p>");
If you don't, you're doing this:
document.write(("<p>The result is " + x) == (y + "</p>"));
And in general, don't use document.write
.
Upvotes: 6
Reputation: 21465
This evaluates to a string:
(x==y).toString();
So you can use:
"<p>The result is " + (x==y).toString() + "</p>"
Upvotes: 3