Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungThe Indie Dev Visibility Playbook: From Zero Users to Your First 100(21.09.2026 um 00:08 Uhr)
Sichere ProgrammierungBase, Chat and Reasoning Models: How Are They Different?(21.09.2026 um 00:09 Uhr)
Sichere ProgrammierungHexfield Deck is for Kanban lovers and Markdown believers(21.09.2026 um 00:20 Uhr)
Sichere ProgrammierungPermissions and Authorisation: A Practical Playbook(21.09.2026 um 00:21 Uhr)
Linux Tipps & Hardeningfilet | Terminal File Manager(20.09.2026 um 21:03 Uhr)
Linux Tipps & HardeningLooking for feedback on my Linux Distro(20.09.2026 um 21:03 Uhr)
Sichere ProgrammierungThe Indie Dev Visibility Playbook: From Zero Users to Your First 100(21.09.2026 um 00:08 Uhr)
Sichere ProgrammierungBase, Chat and Reasoning Models: How Are They Different?(21.09.2026 um 00:09 Uhr)
Sichere ProgrammierungHexfield Deck is for Kanban lovers and Markdown believers(21.09.2026 um 00:20 Uhr)
Sichere ProgrammierungPermissions and Authorisation: A Practical Playbook(21.09.2026 um 00:21 Uhr)
Linux Tipps & Hardeningfilet | Terminal File Manager(20.09.2026 um 21:03 Uhr)
Linux Tipps & HardeningLooking for feedback on my Linux Distro(20.09.2026 um 21:03 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

🧠 JavaScript Type Coercion — A Question That Teaches

Reagiere als Erste:r — dein Feedback zählt!

Let’s talk about a JavaScript expression that looks wrong but is 100% valid 👀

[] == ![]

At first glance, most people expect this to be false.

👉 But the result is true.

Let’s break it down step by step, using JavaScript’s actual rules — no magic, no guessing

Step 1: Evaluate the logical NOT

![]
  • [] is an object
  • All objects in JavaScript are truthy
  • Applying ! to a truthy value results in false

So the expression becomes:

[] == false

Step 2: Loose equality (==) triggers type coercion

In JavaScript's loose equality (==) logic, if one of the operands is a Boolean, it is always converted to a Number before the comparison continues.

The Conversion Rule

According to the ECMAScript specification, the process for [] == false (for example) looks like this:

  • Boolean to Number: false becomes 0. (Conversely, true becomes 1).
  • The Resulting Comparison: Now the engine is looking at [] == 0.
  • Object to Primitive: Since one side is a number and the other is an object (the array), JavaScript triggers the ToPrimitive process on the array
[] == 0

Step 3: Object-to-primitive conversion

  • When using the loose equality operator (==) to compare an object to a primitive, JavaScript uses the "default" hint, which almost always behaves like the Number sequence:
  • valueOf() is called first. (For most plain objects and arrays, this just returns the object itself).
  • toString() is called second because valueOf didn't provide a primitive.

For an empty array:

[].toString() // ""

So now the comparison becomes:

"" == 0

Step 4: Final coercion

In a string vs. number comparison, the string is converted to a number. Number("") is 0.

"" (empty string) → 0

Comparison becomes:

0 == 0

✅ Result: true

🔑 Key Takeaways

  • JavaScript follows strict, deterministic coercion rules
  • == allows implicit conversions that can be surprising
  • Arrays convert to strings
  • Booleans convert to numbers
  • This behavior is predictable once you know the rules
  • This is exactly why === is recommended in most production code.

Type Coercion with the + Operator

🔹 Case 1: [] + 1

👉 Result: "1"

Why?

  • + is special in JavaScript It can mean addition or string concatenation
  • When one operand becomes a string, concatenation wins
  • [] → "" (empty string)

Expression becomes:

"" + 1 → "1"

Type Coercion with the - Operator

🔹Case 2: [] - 1

👉 Result: -1

Why?

  • - is numeric only
  • JavaScript forces both sides to numbers
[] → "" → 0

Expression becomes:

0 - 1 → -1

🚀 Challenge (Object Comparison)

Now that we understand arrays, here’s a bit tougher one:

{} == !{}
{} - 1
{} + 1

Same language. Same coercion rules.

👉 What do you think the output is — and why?

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten 🧠 JavaScript Type Coercion — A Question That Teaches

Thematisch verwandte Begriffe: JavaScript, Type, Coercion, Question · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94084 | Suricata before 8.0.7 has an Http2ThreadMultiBuf use-after-free when a t…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick