Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Why is no error thrown when I call this code?

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?

like image 354
AndreKuntze Avatar asked Dec 21 '22 20:12

AndreKuntze


1 Answers

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;
like image 134
Paul Avatar answered Dec 23 '22 09:12

Paul