/ javascript

The Comma Operator in JavaScript

The comma operator takes two expressions, evaluates both and then returns the evaluation of the right expression. For example:

const returnLeft = (x, y) => (y, x)
returnLeft(3, 5) // 3

It's important to keep in mind that both expressions will be evaluated, meaning that you have to be aware of the possible side-effects of the first expression passed.

For example:

const log = (y) => console.log(`The value of y is ${y}`)
let y = 1
returnLeft(y, log(y))
// The value of y is 1
// 1

Image Credit: mercibeaucoup,