How Javascript Sees Your Code: Statements, Expressions & the Semicolon Mystery


Ever wonder how Javascript interprets your code under the hood?
Let's zoom out and see what the interpreter sees โ and why it matters.
When we write Javascript, it's easy to think of it like a list of commands:
let name = "Alex";
console.log(name);
But Javascript doesn't just run these commands mindlessly.
It parses, evaluates, and sometimes... "fixes" our mistakes ๐ค
That's where concepts like statements, expressions, comments, and the infamous semicolon debate come in.
Let's break it all down โ including some surprising behaviour from something called Automatic Semicolon Insertion (ASI).
๐งฑ Statements vs Expressions โ What's the Difference?
Understanding the distinction between statements and expressions is not just important, it's foundational.
Everything in Javascript, from the simplest to the most complex, is either a statement or an expression. This knowledge will empower you to write more efficient and effective code.
โ A Statement:
A command. It does something.
let age = 30;
if (age > 18) {
console.log("Adult");
}
let
, if
, for
, return
โ these are statements.
๐ง Think of statements as sentences. They execute an action.
โ An Expression:
A unit of code that produces a value.
5 + 5; // 10
"Hi" + "JS"; // "HiJS"
Math.max(2, 4); // 4
Even this is an expression:
let sum = 2 + 3;
Here, 2 + 3
is an expression. The full line is a statement.
๐ง If it returns something, it's an expression. If it just does something, it's likely a statement.
๐ Expressions Inside Statements
Often, we use expressions inside statements:
if (user.age > 18) { // expression: user.age > 18
console.log("Welcome");
}
So remember:
An expression becomes part of a larger statement
Expressions return values; statements perform actions
๐ฌ Comments โ Talking to Yourself (and Others)
Javascript lets us leave notes in our code using comments โ and we should!
โ Single-Line Comments:
// This is a one-line comment
let score = 10; // You can also put it after the code
โ Multi-Line Comments:
/*
This is a block comment.
It can span multiple lines.
Useful for documentation!
*/
Use comments to:
Explain why you're doing something (not just what)
Leave reminders (// TODO: clean this up)
Temporarily disable the code
๐ง Good comments are gold.
โ Semicolons: Optional, Required, or What?
Here's the thing: Javascript lets us skip semicolons, but should we?
let x = 5
let y = 10
console.log(x + y)
This works. But here's the twist: it works because Javascript inserts the semicolons for us... most of the time.
Let's unpack that.
๐ง Automatic Semicolon Insertion (ASI) โ Javascript Fixes Your Code?
Javascript has a rule-based feature called ASI โ Automatic Semicolon Insertion.
When the interpreter reads your code, if it sees a line break where it expects a semicolon, it'll often insert one for you.
Great, right?
Not always.
โ Classic ASI Gotcha:
return
{
message: "Hello"
}
We think this returns an object.
Javascript sees it as:
return; // <-- ASI inserts semicolon here
{ message: "Hello" } // This becomes a block, not an object
So it returns undefined
, not the object we expected ๐ฌ
โ When ASI Works Fine:
Simple variable assignments
Console logs
Function calls
But...
โ When ASI Breaks Stuff:
return
statements on a new lineIIFEs (Immediately Invoked Function Expressions)
++
or--
in specific line-break situations
๐ Should We Use Semicolons?
Some teams say, "Skip them, the interpreter handles it."
Others say, "Always use them for safety."
Here's our take:
โ๏ธ Use them. It avoids edge cases, clarifies intent, and saves debugging time.
It's one character. We'll live ๐
โ ๏ธ Real Gotchas We Should Know
Let's look at a few more subtle issues where the way Javascript parses code can trip us up.
โ ASI Misleading Errors:
let b = "ASI Gotcha!"
const a = b
(1 + 2).toString() // TypeError: b is not a function
Javascript might read this as a function call on b
, not on 1 + 2
.
Running it in console,
Adding a semicolon solves it:
let b = "ASI Gotcha!"
const a = b;
(1 + 2).toString(); // '3'
๐ Reference Specs (For the Curious)
If we want to go full deep-dive mode, check out:
ECMA-262 Specification โ the official Javascript language spec
This is where the language rules are defined. Hardcore, but worth knowing exists.
๐ง Recap โ What We've Learned
Statements perform actions; Expressions return values
Comments help us explain intent, document behaviour, and improve readability
Semicolons are mostly optional, but ASI can misinterpret code, especially with return
Use semicolons consistently to avoid bugs and make intent clear
Javascript does a lot of interpretation โ but it's not magic. It follows strict parsing rules.
๐ Next Up:
๐ Time to talk about Variables, Declarations and headaches ๐
Subscribe to my newsletter
Read articles from Sangy K directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sangy K
Sangy K
An Introvert Coder | Trying to be a Full-stack Developer | A Wannabe Content Creator