RSS Amplifier

Christoph Ryan · Nov 3, 2025

🧩 The Odd JavaScript Quirk: Why 'Hello' + 1 + 2 Isn't the Same as 1 + 2 + 'Hello'

0
Sign in to vote or save

Christoph Ryan · Christoph Ryan

I recently stumbled upon this quirky little JavaScript behavior while playing around with string concatenation — and honestly, it reminded me why programming sometimes feels like a game with hidden rules.

Here’s what I tried:

console.log(’Hello’ + 1 + 2);
console.log(1 + 2 + ‘Hello’);

And here’s what I got:

Hello12
3Hello

At first glance, that looks weird, right? Both lines use the same ingredients — a string and two numbers — yet JavaScript cooks them into totally different results.

The key here is something called type coercion, which means JavaScript automatically converts values between types depending on context.

The + operator in JavaScript is special — it can either add numbers or concatenate strings.
So when JavaScript sees ‘Hello’ + 1, it converts 1 into ‘1’, resulting in ‘Hello1’. Then it adds 2, which becomes ‘2’, so the final output is ‘Hello12’.

But if the numbers come first:

1 + 2 // → 3
3 + ‘Hello’ // → ‘3Hello’

Order changes everything.

For a deeper breakdown, Mozilla’s official guide on Type Conversion explains this perfectly.

These small quirks aren’t just trivia — they can cause subtle bugs in real-world projects.
Imagine writing:

let score = 10;
console.log(’Your score: ‘ + score + 5);

Expecting “Your score: 15”, but instead you get “Your score: 105”.

The fix?

console.log(’Your score: ‘ + (score + 5));

A tiny pair of parentheses can save hours of debugging.

Interestingly, odd behaviors like this aren’t unique to JavaScript.

When I was exploring new database technologies like FaunaDB and SurrealDB, I noticed how some of their query structures feel “off” at first — until you understand their design logic.

The same goes for on-device AI models — what seems strange at first (like running AI locally) ends up making perfect sense once you realize how much privacy and efficiency it adds.

Even in tools like Excel, small logic quirks can flip results entirely. I’ve talked about that in my post on conditional formatting automation.

I first thought this ‘Hello’ + 1 + 2 vs 1 + 2 + ‘Hello’ thing was just another JavaScript meme — but it actually reflects something deeper: JavaScript’s flexible nature comes with a trade-off in predictability.

If you’re coding in JS, use parentheses generously.
It’s a small habit that keeps your logic clear and your output sane.

👉 Read the full deep-dive article here:
The Odd JavaScript Quirk: Why ‘Hello’ + 1 + 2 Isn’t the Same as 1 + 2 + ‘Hello’

No posts

Read the original on christechno.substack.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.