+ 1
Difference between Comma and Semicolon in JS
Hi, in some JS codes I noticed that when there are multiple functions one after the other there is a comma that separates them and not a semicolon (this is only present in the last function that is executed), I would like to know what changes if I use a semicolon instead of a comma.
8 Answers
+ 4
Found these too.
https://stackoverflow.com/questions/8506305/js-statement-separator-with-commas
https://javascriptweblog.wordpress.com/2011/04/04/the-javascript-comma-operator/
I don't think it is very practical to use this style in a normal program, unless you really just want to obtain the last result from a group of expressions.
+ 4
"I also noticed that the comma is interpreted as an or || operator in Boolean expressions.
for example these two instructions return the same value (false):'"
alert((null == this.get_currentIndex() && this.update_select() || this.check_convert_result(e)));
alert((null == this.get_currentIndex() && this.update_select(), this.check_convert_result(e)));
Those two lines may indeed return the same value, but I believe they work in different way.
The first one considers the return value from get_currentIndex(), update_select() and check_convert_result()
The second one only considers the return value from check_convert_result() because the evaluation result of the left hand side operand - which involved get_currentIndex() and update_select() - were discarded, as we have already know that's how the comma operator behaves, to only care for the operator's right-most operand : )
+ 2
You need to show some code to demonstrate what you mean.
Comma and semicolon are not interchangable, they have completely different syntactic meaning.
The semicolon marks the end of a statement, but the Javascript interpreter can usually add it automatically at the end of each line, so most of the time your code will work even without using any semicolons.
The comma is used to separate arguments of a function, or elements of an array.
+ 2
The comma operator ensures that expression evaluation will occur left-to-right, and it returns the rightmost result. You may find that it is used in for loops that have more than one loop index, and especially where the second index depends on the value of the first index. In that case the comma operator is used to ensure that the first index gets updated before the second index.
for (val i=0, j=0; i<n; ++i, j=2*i)
If you are seeing code that uses a comma operator where a semicolon ; could be used instead, then you would be wise not to imitate that style.
Note that a comma operator is not the same as the comma used in an argument list for a function call. Each of those values are passed to the function, whereas a comma operator only evaluates as the rightmost result.
+ 2
For example:
document.body.appendChild(n),
n.click(),
n.remove();
As you can see there are commas instead of semicolons, I would like to understand why they were used.
+ 2
The minifier reduces the number of characters in the code and thereby improves load time. One goal of a minifier also is to improve speed of parsing/interpretation. It may be that the developers of the minifier have determined the comma operator is faster to translate or execute than individual statements, though I am only guessing why they did that.
+ 1
I would like to point out that these lines were taken from a minified single-line js file
0
Brian might be the right answer, I also noticed that the comma is interpreted as an or || operator in Boolean expressions.
for example these two instructions return the same value (false):
alert((null == this.get_currentIndex() && this.update_select() || this.check_convert_result(e)));
alert((null == this.get_currentIndex() && this.update_select(), this.check_convert_result(e)));