Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the comma operator in JavaScript

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I would like to talk to you about how the comma operator is used in JavaScript. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.

Comma operators can play a significant role in conjunction with a set of expression operators, but the evolution of their use is confusing.

Part of the reason for this is that commas have many other meanings in JavaScript.

So in order to make people aware, commas are simple comma operators when participating in the following two expressions: the left-hand expression and the right-hand expression.

These expressions also contain other operators, variables, and functions.

What is the purpose of the comma operator

It joins two expressions sequentially, first estimates all operands from left to right, and then returns the value of the last Operand.

It is important to note that comma operators are different from commas in arrays, objects, and function parameters.

Let x = 10 x = (Xmuri, x); console.log (x); / / expected output: 9x = (20,30); console.log (x); / / expected output: 30

Parentheses must be used in the above example, because the comma operator has the lowest precedence among all JavaScript. Without parentheses, the expression might be modified to look like this:

X = (20), 30

The above statement ends up assigning 20 to X and rounding off the value of the expression on the right. It's natural to wonder why you use parentheses to assign values. It would be nice to assign a value directly.

The answer is that some operators and most functions have side effects. For instance,

Varr = (console.log (1), console.log (2), console.log (3), 4)

You can see that 1 and 2 are displayed on the control panel, and 4 is assigned to the variable varr. In the above example, if you want to replace the comma operator, you can also use a semicolon like this:

Console.log (1); console.log (2); console.log (3); varr = 4

The key here, however, is that semicolons separate statements, while commas separate expressions, and sometimes statements are also expressions.

Sample display

Var var1, var2, var3;var1 = var2 = 10, var3 = 20 var1 / Returns in console console.log (var1); / 10 (left-most) var1 = (var2 = 30, var3 = 40); / / Returns 6in console console.log (var1); / / 40 (right-most)

Calculation of property keys: (this example is quite tricky)

Const map = {[1]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report