Skip to content

Instantly share code, notes, and snippets.

@Sharaal
Last active March 1, 2019 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sharaal/0a86afbd4972433d79fa29b586f72064 to your computer and use it in GitHub Desktop.
Save Sharaal/0a86afbd4972433d79fa29b586f72064 to your computer and use it in GitHub Desktop.
Errors because of omitting ;
/**************************************************
* Immediatly executing anonymous function
**************************************************/
// Results in error "TypeError: console.log(...) is not a function"
console.log('example')
(async () => {})()
// With using of ; it works
console.log('example');
(async () => {})()
/**************************************************
* Destructioning
**************************************************/
// Results in error "ReferenceError: c is not defined"
let a, b
const c = [1, 2]
[a, b] = c
// With using of ; it works
let a, b
const c = [1, 2];
[a, b] = c
@Sharaal
Copy link
Author

Sharaal commented Mar 1, 2019

A detailed article about the automatic semicolon insertion (ASI) feature: http://inimino.org/~inimino/blog/javascript_semicolons

@Sharaal
Copy link
Author

Sharaal commented Mar 1, 2019

A good remember to avoid the problems:
just remember to start lines with ; when they start with [ or (

So the both use cases would looks like:

// Immediatly executing anonymous function

console.log('example')
;(async () => {})()

// Destructioning

let a, b
const c = [1, 2]
;[a, b] = c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment