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).
- An empty statement
- var statement
- An expression statement
- do-while statement
- continue statement
- break statement
- return statement
- 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'
};
}