Sitemap
Izak T

Izak T

founder of web it easier the door to an easier digital future

The if Statement Comment Trap That Makes Safe Code Dangerous

3 min readJun 27, 2026

--

Press enter or click to view image in full size
Photo by Daniil Komov on Unsplash

A few lines of JavaScript can look perfectly safe — until they’re not.

Here’s a small gotcha that’s all about how if statements and comments interact.

The code that looks fine

let stored = localStorage.getItem('jobs')
if (stored)
// return JSON.parse(stored)
stored = JSON.parse(stored)
console.log('Loaded jobs:', stored)

At a glance, you might think: “If stored is truthy, parse it. Otherwise, skip it.”

But that is not what happens.

What actually happens

if (stored)
// return JSON.parse(stored)
stored = JSON.parse(stored)

Because there are no curly braces, the if only controls the next single statement. But here’s the twist: a comment is not a statement. The comment line is ignored entirely, so the if applies to the next executable statement — which is the commented-out return line, not the line below it.

That still sounds like what we want, but the indentation is misleading. The comment sits between the if and the next line, and readers naturally treat indented code as “inside” the if. The parser does not. It sees the comment as nothing, and the next executable statement becomes unconditional:

if (stored)
// return JSON.parse(stored) // comment ignored
stored = JSON.parse(stored) // ← OUTSIDE the if, runs every time

JavaScript does not care about indentation. The parser sees:

if (stored)
// comment (ignored)
stored = JSON.parse(stored) // runs unconditionally

So stored = JSON.parse(stored) runs even when stored is null. JSON.parse(null) is actually fine — it coerces null to the string “null”, which is valid JSON, and returns null:

JSON.parse(null) // null

But if stored is undefined, then JSON.parse(undefined) throws:

SyntaxError: Unexpected token 'u', "u" is not valid JSON

Why? Because undefined is coerced to the string “undefined”, and “u” is not valid JSON. The real bug here is the unconditional execution: whatever stored is, it gets parsed when it should not be parsed at all. That turns a safe “no data” path into a potentially dangerous “parse anything” path.

The fix: always use braces

let stored = localStorage.getItem('jobs')
if (stored) {
stored = JSON.parse(stored)
}
console.log('Loaded jobs:', stored)

With braces, the intent is explicit and the bug is impossible.

A related trap: declaring a variable inside the block

Adding braces fixes the comment bug, but it introduces a second trap. Once you have an `if` block, you might try to declare a new variable inside it:

let stored = localStorage.getItem('jobs')

if (stored) {
const parsed = JSON.parse(stored) // lives inside the block
}

console.log(parsed) // ❌ ReferenceError: parsed is not defined

Why? Because `let` and `const` are block-scoped. Both mistakes come from the same misunderstanding: what counts as “inside” the `if` and what survives after it.

Get Izak T’s stories in your inbox

Join Medium for free to get updates from this writer.

Think of the `{}` block as a box. A variable declared inside the box stays inside the box. Once the box closes, the variable is thrown away. The braceless `if` tricked us into putting code outside the box; now the box is correct, but a variable placed too deep inside the box still cannot escape.

If you need the value outside the box, create the variable outside the box first, then change it inside:

let stored = localStorage.getItem('jobs')

if (stored) {
stored = JSON.parse(stored) // same variable, new value
}

console.log(stored) // ✅ works

Now `stored` lives in the outer scope. The `if` block borrows it, converts the string to an array, and the outer scope still has access after the block ends.

The one-liner alternative

For simple cases, a ternary keeps everything on one line and avoids the block issue entirely. I use a separate variable name here because the raw string and the parsed array are different shapes of data:

const rawStored = localStorage.getItem('jobs')
const parsedJobs = rawStored ? JSON.parse(rawStored) : []

Key takeaways

  1. Always use {} with if statements. It removes ambiguity and prevents comment-line bugs.
  2. JavaScript ignores indentation. Only braces define block scope.
  3. Comments are not statements. A comment after an if does not “block” the next line from being inside the if.
  4. let/const are block-scoped. If you need a value outside the block, declare it outside and reassign it inside.

A quick rule of thumb

If you find yourself writing `if` without braces, add braces. The extra two characters can save you from a runtime error that looks impossible at first glance.

--

--

founder of web it easier the door to an easier digital future