mika-el
5/23/2019 - 10:05 AM

Comma operator

// JavaScript has a comma operator. It allows us to write multiple expressions separated by comma in a single line and return the result of last expression

// syntax
// let result = expression1, expression2,... expressionN
// Here, all the expressions will get evaluated and the result variable will be assigned the value returned by expressionN.

// You might have already used Comma operator in a for loop

// for (var a = 0, b = 10; a <= 10; a++, b--)
// Sometimes it helps when writing multiple statements in a single line

function getNextValue() {
    return counter++, console.log(counter), counter
}
// or writing short lamda functions

const getSquare = x => (console.log (x), x * x)