BY Simmi Kava19 May 2021 Edit
JavaScript: Sequence Expression - The Comma Operator (,)

Comma Operator(,)


Is the comma operator same as comma within arrays, objects, and function parameters and arguments?

NO! It is completely different from them. Comma operator is expression separator where as semicolon (;) is a statement separator.

This operator evaluates each operand value from left to right and returns the last operand. As shown in the example, when we execute the valueOf() method on the Object, it returns the value as 30 i.e. last value of the operand.

Syntax

expr1, expr2, expr3…

What is it's use?

If you want to supply multiple parameters in a for loop, this can be handy. It also allows you to use multiple expressions in a place where one expression is expected. Examples:

  • array swapping without using a temporary variable.
  • console.log statements
  • Arrow (lamda) function
  • Array reduce() method

Examples

Runtime Algorithm:

  1. Evaluate expression (exp)
  2. Perform ?GetValue(exp)
  3. Evaluate Assignment Expression (res)
  4. Return ?GetValue(res)

Ref: ECMAScript Comma Operator

GetValue() must be called irrespective of the value being used or not; as it may have observable side-effects.

Note that, Operator precedence and associativity rules are applied to determine how operators are parsed.

Happy Learning!

BY Simmi Kava
LikeCommentSave
LikeCommentSaveShare
0
Categories
General
YantraJS
Developer Guides
Tutorials
Web Atoms Updates

POPULAR POSTS
17 Mar 2021
LATEST ACTIVITY
Simmi Kava
commented this post.
Simmi Kava
liked this post.
Show more
ARCHIVES
2025
2024
2023
2022
TAGS
javascript (56)
developer (25)
javascriptdeveloper (16)
Xamarin.Forms (16)
Html (14)
typescript (12)
webatoms (12)
xamarin (11)
coding (10)
web-atoms (10)
arrays (9)
android (8)
javascript-developer (8)
csharp (7)
dotnet (7)
css (6)
update (6)
dotnet-standard (5)
function (5)
iOS (5)
methods (4)




Web Atoms: JSX (TSX + TypeScript) for Xamarin.Forms, Hot Reload Your App in Production Environment

PlaygroundSamples Repository