Talking about the usage of comma operator in JS

  • 2021-06-28 10:17:18
  • OfStack

Be careful:

1. Since you are currently working on JavaScript technology, take JavaScript as an example.You can try it yourself in PHP.

2. JavaScript syntax is complex, so take JavaScript as an example.

Recent re-reading of JavaScript's authoritative guide book, should be said to have read it carefully, so I want to record more of what I've learned.after

I will gradually write more articles about this book.

The theoretical knowledge in this article comes from the JavaScript authoritative guide, which I'll do here for a moment, or take notes.

If you have a good foundation, it's okay to fully understand, but if you read a bit depressed, add my QQ:76863715

The premise of reading this article is that you can distinguish what is an expression from what is a statement.There are also clear operators and arithmetic.place

The predicate's expression is -- an JavaScript phrase that the JavaScript interpreter can calculate to generate a value.Expressions can

They are divided into the following three types:

1) Direct quantities, such as 1.7 being digital, JavaScript Authoritative Guide being a string direct, etc.

2) Variables

The value of a direct expression is the direct quantity itself, and the value of a variable expression is the value stored or referenced by the variable.

3) Simple expressions mentioned above can be "merged" to create more complex expressions.For example, 1.7 is an expression, i is also an expression, and the following code shows the same (or can be called) expression:

i + 1.7

The value of this expression above is the sum of two simple expressions (one variable expression and one simple expression).In this example,'+'is an operator that combines two simple expressions to form a complex expression.

Number of Operators

Operators can be classified according to the number of operands required by the operator. Most operators are binary operators, which combine two "expressions" into a complex expression.In short, it has two operands.In addition, JavaScript supports a large number of unary operators, which can convert one expression into another, more complex expression.For example, in expression-3, the operator'-'is a unary operator, and it performs the opposite operation.

JavaScript also supports the 3-ary operator'?:'.It combines three expressions into one complex expression.

OK, the comma operator is explained below.

A comma operator that first evaluates the parameters on the left, then the values on the right.It then returns the value of the rightmost parameter.

The example given in the original book is not good enough to explain the above sentence. Here is another one:


<script>
var a = 10, b = 20;

function CommaTest(){
return a++, b++, 10;
}

var c = CommaTest();

alert(a); //  Return 11
alert(b); //  Return 21
alert(c); //  Return 10

</script>

The value of the variable c is the value returned by the function CommaTest, while a and b add one more.

Conflict between Comma Operator and Function Call Operator

In JavaScript, a function call is really a function call operator.It's special because it's often never used in other programming language materials.It then has no fixed number of operands.

The first argument to the function call operator is a function name or an expression that references the function followed by parentheses ().The middle of parentheses can be an indefinite number of operators, which can be any expression separated by commas.

The function call operator calculates every operand of it, the first operand is specified as the function name (before parentheses), and the values of all the operands in the middle of the parentheses are passed to the function as its parameters.

For example:


document.close()
Math.sin(x)
alert("Welcome " + name)
Date.UTC(2000, 11, 31, 23, 59, 59)
funcs.f(funcs.args[0], funcs.args[1])

Now that you know how to call function operators, let's give an example of how to handle their conflicts.


<script>
alert(2*5, 2*4); //  output 10
</script>

The above code outputs 10, but if interpreted according to the principles of the comma operator, it should be output 8.Why?

Since the comma operator has the lowest priority in JavaScript, it is useful to keep this in mind.So the function call operator will run before the comma operator.Result The alert function outputs the value of the first parameter.Modify the above code to look like this.


<script>
alert((2*5, 2*4)); //  Return 8
</script>

Conflict between comma operator and assignment

In JavaScript, the comma operator has a lower precedence than the assignment operator.See the code below.


<script>
var a = 20;
var b = ++a,10;
alert(b);
</script>

This code does not seem to run, probably because the assignment operator takes precedence over the comma expression if you change the code to


<script>
var a = 20;
var b = (++a,10);
alert(b);
</script>

That's it.

The above mentioned "possibilities" can be explained here. This is my own view, not authoritative.

The comma operator requires that its operand be a complex or simple expression (such as a variable or a direct quantity), but because the assignment operator takes precedence over the comma operator, it becomes a statement with the var keyword instead of an operand or an expression on the left

Code that previously could not be executed can be seen as follows:


<script>
var a = 20;
(var b = ++a),10;
alert(b);
</script>

There are expression statements in a statement, but not all statements are expressions.

#################################################

1. The Characters and Functions of Comma Operators

The purpose of the comma operator is to join several expressions together.It has the lowest priority among all operators and the combining direction is left to right.

For example: 3*3, 4*4

2. Comma Expression

Comma expressions have the general form: expression 1, expression 2, expression 3...expression n

The comma expression is solved by first calculating the value of expression 1, then the value of expression 2,... 1 until the value of expression n.The final value of the entire comma expression is the value of the expression n.

Here are a few examples:

x=8*2, x*4 /* The value of the entire expression is 64, and x is 16*/

(x=8*2, x*4), x*2 /* The entire expression has a value of 128, and x has a value of 16*/

x=(z=5,5*2)/* The whole expression is an assignment expression, which has a value of 10, and z has a value of 5*/

x=z=5,5*2 /* The entire expression is a comma expression with a value of 10 and values of x and z are 5*/

Comma expressions aren't used much, 1 is usually used when you assign an initial value to a loop variable.So not all commas in a program should be considered comma operators, especially when a function is called, the parameters are separated by commas, and then the comma is not a comma operator.

For example: printf ("%d,%d,%d"), x, y, z);

###################################################

Operators make the expressions on both sides execute in left-to-right order and get the values of the expressions on the right side.The most common use of operators is in the incremental expression of the for loop.For example:


for (i = 0; i < 10; i++, j++)
{
k = i + j;
}

The for statement allows only a single expression to be executed each time it passes through the end of the loop.Operators are used to circumvent this limitation by allowing multiple expressions to be treated as a single expression.


Related articles: