Statements, Semicolons, and Whitespace
Understand how JavaScript statements, semicolons, and whitespace work together.
Statements
A statement is a single instruction the JavaScript engine can execute. For example:
console.log("Hello, World!");Statements can declare variables, call functions, or perform calculations.
Good to Know
Semicolons
Semicolons end statements. JavaScript can sometimes insert them for you, but adding them explicitly is best practice.
let age = 18;Style guides like Airbnb or Google generally recommend always using semicolons.
You can also place multiple statements on the same line if you separate them with semicolons:
let x = 10; let y = 20;
console.log(x + y);Whitespace
Whitespace is any space, tab, or blank line in your code. JavaScript ignores extra whitespace, letting you format code for readability. These are identical in functionality:
let name = "Shyam"; // best practiceThis is why consistent whitespace matters:
- Makes code easier for humans to scan
- Avoids confusion in teamwork
- Follows industry style guides
Therefore, remove any extra whitespaces from code and make it more readable:
let name = "Shyam" ;
let name = "Shyam"; Tip
Line Breaks
JavaScript separates statements using line breaks, but automatic semicolon insertion can sometimes cause mistakes. Always using semicolons is safer:
If you leave out semicolons, JavaScript might still work thanks to automatic semicolon insertion, but it's riskier:
let score = 42;
console.log(score);This can lead to unexpected behavior, especially in complex code. Always using semicolons is safer and clearer.
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on