I am playing around with the HTML canvas and write some text on it using Javascript.
While doing this I made a simple error which took me some time to find. I wrote:
context.fillText = ("My message", x-coord, y-coord);
The equal sign prevented the behavior I expected. But there is one thing I do not get: Why does this code not give me an error in the Javascript console of Chrome?
Is this valid Javascript? If yes: Could you explain whaat the code does when the equal sign is there?
Yes that is valid Javascript. It is using the comma operator, which just evaluates the expression on the left, then the one on the right and returns the value of the one on the right.
Since the expressions "My message"
and x-coord
have no side-effects, it is the same as:
context.fillText = y-coord;
Or:
"My message"; // Does nothing
x-coord; // Does nothing
context.fillText = y-coord;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With