Why does javascript accept commas in if statements?

后端 未结 5 1021
北荒
北荒 2020-11-29 23:47

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         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 00:04

    commas in javascript are actually pretty arcane. The coolest use I have seen is this

    while(doSomething(), checkIfSomethingHappened());
    

    the most common would be the way var is used in modern js

    var foo = 1,
        bar = 2;
    

提交回复
热议问题