1.

How can I replace newlines with spaces in JavaScript?

Answer»

The comma operator in JavaScript evaluates each of its operands. It returns the value of the LAST operand.

  • Multiple Expressions

With the usage of comma operator, you can easily add multiple expressions. The syntax:

exp1, exp2, exp3, ……exp

Here, exp1, ex,2 exp3 are the expressions.

  • Multiple parameters in for loop

Add multiple parameters in a for loop USING the comma operator:

for (var a = 0, b = 5; a <= 5; a++, b--)
  • Return Statement

Use the comma operator in a return statement. Process before returning using comma:

FUNCTION show() {   var x = 0;   return (x += 1, x); }
  • Declare multiple variables at once

With comma operator, you can declare multiple variables at once. Let’s say you have three variables. To declare and initialize them on NEW lines, you can declare them in a single LINE as shown below:

var a = 5, b = 10, c = 30;
  • Multiple arguments in a function call

You can also use the comma operators to call multiple arguments in a function call. Let’s say you have a function “calculate” and you need to find the multiplication of three numbers. For that, use the comma operators

calculate(33, 47, 79);


Discussion

No Comment Found