Can anyone explain why there's a leading semi-colon in the inline script example? AFAICT it doesn't do anything.
function hasSemicolons() {
console.log('hello world');
}
(function() {
console.log('this is broken, JS will call hasSemicolons()');
})()
In the ImmortalDB example, since it's a standalone <script> tag this is just extra defensive against a minifier that would join the tag with other tags, but just wanted to call out that there's a reason to include the leading semicolon in front of an IIFE whether or not you write your JavaScript with semicolons.See also: https://stackoverflow.com/questions/1873983/what-does-the-le...
console.log('this doesn't work!')
(false ? console.log : console.error)('this will fail with a TypeError')
>> Uncaught TypeError: console.log(...) is not a function
console.log('this works!')
;(true ? console.log : console.error)('hello world')Since everything in js is a statement the correct semi-colon syntax for the above is
function hasSemicolons() {
console.log('hello world');
};
(function() {
console.log('this is broken, JS will call hasSemicolons()');
})();