Semicolon after object required?

Hi,
Moving on to the first exercise in the object section of the JavaScript course I noticed that I didn’t put a semicolon after my object yet the code still executed. Is this required? or was it only required for older versions?

let address = {
    street: 'Kinabalu Drive',
    city: 'Tamborine Mountain',
    zipCode: 4272
}

function showAddress(address) {
    for (i in address)
        console.log(i, address[i]);
}

showAddress(address);

Nope, not required, and neither are your other two semicolons.

Technically, the semicolons are required, but because of a language feature called automatic semicolon insertion, if you don’t include the semicolons, they’ll be added for you.

1 Like

Thanks,
Also noticed there doesn’t seem to be a need to declare a variable in a for statement and some others.

for (i in address)

does this matter that not for (let i in address) ?

I guess a better question to ask would be if I don’t use standards and insert semicolons and declare variables etc, Then would there be performance issues in large scale programs?

this also works:

anotherAddress = {
    street: 'Kinabalu',
    city: 'Tamborine',
    zipCode: 000
}

What’s happening with i and anotherAddress is that they are being added as properties to the global or window object, which is probably not what you intended! You may want to consider using a linter, such as eslint, to catch these sorts of things. You can even configure it to detect missing semicolons.

I doubt there is much, if any, noticeable performance implication on relying on automatic semicolon insertion. Some folks actually prefer not using semicolons and use libraries like standard JS to keep their code relatively semicolon free.

1 Like