Why so many semicolons in JavaScript?

前端 未结 10 1806
夕颜
夕颜 2021-02-13 03:22

I tend to be a prolific user of semicolons in my JavaScript:

var x = 1;
var y = 2;
if (x==y){do something};

I recently noticed that I was looki

10条回答
  •  忘掉有多难
    2021-02-13 04:04

    Whether to use semicolons or not is largely a matter of choice. However, in javascript, unlike many other languages, not using them can lead to unexpected results. For example,

    return
        {'foo':'bar'};
    

    will, because of javascript's automatic semicolon insertion, return nothing.

提交回复
热议问题