The Confusing Comma In JavaScript |
Written by Ian Elliot | ||||
Thursday, 13 June 2019 | ||||
Page 2 of 3
This Is Not The Comma You Are Looking ForOne reason for confusion is that there are lots of places where commas are just separators. For example, in object literals:
the commas are just separators. In arrays:
the commas are just separators. Perhaps most confusing of all is the var declaration. For example in:
the commas are just separators, not comma operators. The reason is that the var statement is a declaration and an initialization equivalent to
Things like a=1 is an expression; the value of the expression is, in this case one. This allows you to write things like:
which is equivalent to a=(b=1) However:
isn't an expression and doesn't have a value. Rather, it contains an expression because initialization is of the form:
or
This is subtle, but if you are following you can see that just by adding a pair of parentheses we can turn the previous var example into something that does use the comma operator:
What do you think is stored in a, b and c? The answer is that a is 3, b is 2 and c is 3. The statement is of the form:
and the expression is:
which, by the rules of the comma operator, evaluates each sub-expression in turn, throws away each of the results except for the last, which is the value of the expression. Notice that the b=2 and c=3 are expressions, and if b and c don't exist they are created as global variables. Now you should be able to see why:
is nonsense and just generates an error, but
is perfectly OK and declares two local varlables a and b. What is stored in b? Answer 3 because the expression is (2,c=3). Using The CommaSo now we have the comma mastered - what can you use it for? The honest answer is not much. There are places, espcially in libraries, where you will find the comma operator in use, but in most cases there are much better and clearer ways of expressing the same idea. The most commonly encountered use of the comma operator is to make more complex for loops. Really for loops should be kept simple so the idea of making them more complex isn't a good one. JavaScript may be a jem but sometimes it is a very dangerous one. A JavaScript for loop has the form:
Expression1 is evaluated once when the loop starts and it is usually where the initialization occurs. Expression2 is evaluated before each new iteration of the loop. If it evaluates to false the loop stops. Expression3 is evaluated at the end of each loop iteration and it is generally where loop counters etc are updated. Notice that you can write any expression you care to in a for loop and you can use the comma operator so that you can write multple expressions. So, for example, what do you think the following for loop is all about?
The first expression isn't a use of the comma operator. It just uses the var statement to create two local variables, i and j, and sets them to zero and ten respectively. The second expression is a simple test for i being less than or equal to j, and doesn't use the comma operator. The final expression is the only use of the comma operator and it adds one to i and subtracts one from j each time through the loop. So the values of i and j we create are:
and then the loop ends. Clever? You could just as easily have written the loop as:
You can use a similar technique to turn a while loop into a sort of for loop:
There are lots of similar uses and examples, but they all come down to the same thing - we need to use a single expression but also need to evaluate some other expressions first just for their side effects. <ASIN:1871962579> <ASIN:1871962560> <ASIN:1871962501> <ASIN:1871962528> |
||||
Last Updated ( Thursday, 13 June 2019 ) |