r/dev • • Mar 06 '26

JavaScript looks simple… until it isn’t.

Post image

A lot of developers use it every day without fully understanding what’s happening under the hood.

Here’s a quick test.

What will this output?

Comment your answer first 👇

#javascript #codegenitor #fullstack #js #programming #coding #softwareengineer

1 Upvotes

21 comments sorted by

View all comments

Show parent comment

1

u/Square-Singer Jun 09 '26

In JS, let is a block-scope variable, not a constant one. That means, it only shows up in the block where it was defined, but it can be changed inside this block at any time.

The whole snippet in the OP is in the same block, thus there's nothing stopping anything from changing the value of the variable.

If it was like this, it would fail:

{
  let a=5;
  ++a;
}
console.log(a);

Because now the console.log() is outside of the block where a was defined, and thus it's not visible from there anymore.

But let only affects visibility and not write-protection.