Asked • 05/06/19

Why does javascript accept commas in if statements?

I stumbled across some javascript syntax that seemed like it should produce a parse error of some kind but doesn't: if (true, true) {console.log('splendid')} else {console.log('horrid')} // splendid if (true, false) {console.log('splendid')} else {console.log('horrid')} // horrid It seems only the last expression affects the logic, though all expressions are executed: if (console.log('super'), true) {console.log('splendid')} // super splendid Anyone know why that is valid javascript syntax? Is there any practical use for it?

2 Answers By Expert Tutors


Sparky E. answered • 07/08/19

4.8 (25)

Software Developer

Mike L. answered • 05/12/19

New to Wyzant

Patient Old Programmer

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.


Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.