syntax - Why so many semicolons in JavaScript? -
syntax - Why so many semicolons in JavaScript? -
i tend prolific user of semicolons in javascript:
var x = 1; var y = 2; if (x==y){do something}; i noticed looking @ lot of javascript doesn't have semicolons next if statements. occurred me don't know preferred syntax semicolons in js , after googling learned (rather surprisingly) there no need semicolons @ aside splitting statements on 1 line.
so, question...where did habit of people using semicolons come from? remnant popular language in utilize @ time javascript came play? practice in general? me?
i'm going stick no other reason it's nice when writing jquery chains spot end.
update:
thanks answers, everyone! looks like, summarize things, reason see lot of semicolons in js when not needed comes various variables:
older js minimizer's produce broken code if did not insert semicolons many other languages utilize them, it's carried-over habit on occasion, semicolon can alter logic some prefer utilize them create code more human-readable
many computer languages utilize semicolons denote end of statement. c, c++, , java popular examples of this.
as why people utilize them despite them beingness optional, improve readability of code. in cases it's done out of habit, need semicolons in code remove possible ambiguity. it's improve safe (and consistent) sorry.
here illustration taken do recommend using semicolons after every statement in javascript?
// define function var fn = function () { //... } // semicolon missing @ line // execute code within closure (function () { //... })(); this interpreted as:
var fn = function () { //... }(function () { //... })(); additionally, semicolons allow javascript packed/minified properly. otherwise statements mushed 1 big mess.
javascript syntax
Comments
Post a Comment