How Comma Operator Is Used Between The Statements?
Solution 1:
It's only parsed as the comma operator when between expressions, not statements. let x = 3
is not an expression; it does not evaluate to any value. Trying to parse it as an expression does not work:
const result = let x = 3;
console.log(result);
The syntax let <variableName> = <expression>, <variableName2> = <expression2>
is not invoking the comma operator - that syntax, when used around let
, const
, and var
, simply initializes multiple variables at once, and said syntax simply happens to use commas, despite the fact that they're not interpreted as the comma operator.
In contrast, with
let x = 3,
y = 5console.log(x,y)
You're not invoking the comma operator here, either: you have a parameter list. To invoke the comma operator, you also need to be not immediately inside a parameter list, eg:
let x = 3,
y = 5console.log((x,y))
or
let x = 3,
y = 5const someVar = (x,y);
console.log(someVar);
Solution 2:
The comma operator in JavaScript evaluates each of its operands. It returns the value of the last operand. Add multiple expressions using the comma operator.
The following is the syntax:
expression1,expression2, ……expression
Yes, you can use the comma operator to add multiple parameters in a for loop:
for (var a = 0, b =5; a <= 5; a++, b--)
You can also use the comma operator in a return statement. Process before returning using comma:
function myFunc() {
var a = 0;
return (a += 1,a);
}
Above you can see the value of a will process and then it will be returned Here is additional details: https://danhough.com/blog/single-var-pattern-rant/
Post a Comment for "How Comma Operator Is Used Between The Statements?"