JavaScript Comma Operator

You are Here:

JavaScript Comma Operator

The comma operator evaluates each of its operands (from left to right) and returns the value of the last operand.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var a = (10, 20); document.write(a); </script> </body> </html>

More Examples

In the following example, the comma operator saves the repetition of the keyword (var).

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var a = 10, b = 20; document.write(a); </script> </body> </html>

In the following example, the comma operator is used for concatenating a string and a variable.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var a = 10; document.write("The value of a : ", a); </script> </body> </html>

In the following example, the comma operator is used to execute multiple functions when the condition in the conditional operator returns true.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p id="point"></p> <p id="point1"></p> <p id="point2"></p> <script> var x = document.getElementById("point"); var y = document.getElementById("point1"); var z = document.getElementById("point2"); function myF1(){ x.innerHTML = "Function 1 is executed"; } function myF2(){ y.innerHTML = "Function 2 is executed"; } function myF3(){ z.innerHTML = "Function 3 is executed"; } var result = (1 < 2) ? (myF1(), myF2()):myF3(); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author