So I was programming some stuff in Javascript and some time later I saw I made a typo in the following piece of code:
(function() {
var someEl = document.getElementById('id-of-some-el'),l <-----
someOtherEl = document.getElementById('some-other-el');
someEl.onclick = function() {
...
};
})();
Notice how the l isn’t supposed to be there. I’ve only tested this in Firefox but, why didn’t I get a syntax error?
You were trying to create two variables inside the
var-statement:The introduction of the comma meant that you created
someElandl:Semicolons at the end of JS lines are optional, so now you have a distinct, second line of code afterwards:
And this is valid because you can assign to variables without explicitly
var-ing them (albeit imbuing slightly different semantics to your program).