InterviewHubPRO
Section Overview
Q465

Question 465 of 521

Part 8: Modern JavaScript APIs, WeakRef, Proxy & Performance (464 - 521)

89%
JavaScript Internals (521 Qs)•Part 8: Modern JavaScript APIs, WeakRef, Proxy & Performance (464 - 521)

Give an example of statements affected by automatic semicolon insertion?

The javascript parser will automatically add a semicolon while parsing the source code. For example, the below common statements affected by Automatic Semicolon Insertion(ASI).

  1. An empty statement
  2. var statement
  3. An expression statement
  4. do-while statement
  5. continue statement
  6. break statement
  7. return statement
  8. throw statement

Practical Code Example

javascript
// Automatic Semicolon Insertion (ASI) Pitfall:
function getUser() {
  return // ASI inserts semicolon here: 'return;'!
  {
    name: 'Sarah'
  };
}
console.log(getUser()); // undefined (NOT the object!)

// Fixed:
function getUserCorrect() {
  return {
    name: 'Sarah'
  };
}