Surya JS Journey

Learn JavaScript through concepts, code, and guided practice.

A focused workspace for concept study, interactive examples, visual explanations, and challenge solving.

Core JavaScriptBeginner

Type Coercion

Type coercion is JavaScript converting one value type into another during operations or comparisons.

JavaScript sometimes converts values automatically. For example, string concatenation can convert numbers to strings, and loose equality can coerce values before comparing them. Understanding coercion helps you predict odd outputs and write code that is explicit instead of surprising.

Explanation

JavaScript sometimes converts values automatically. For example, string concatenation can convert numbers to strings, and loose equality can coerce values before comparing them. Understanding coercion helps you predict odd outputs and write code that is explicit instead of surprising.

Key Points

  • The `+` operator concatenates if one side is a string.
  • Loose equality may convert values before comparing them.
  • Explicit conversion with `Number`, `String`, or `Boolean` is usually clearer.

Common Mistakes

  • Expecting `"5" + 1` to perform numeric addition.
  • Using loose equality without understanding the conversion rules.
  • Confusing truthy or falsy behavior with actual boolean values.

Practice Workspace

type-coercion.js

Edit the code, run it in the browser, and inspect the console output below. Reset restores the original snippet for this page.

Editor

3 lines

Output

0 entries

Mode

practice

Workspace Notes

Changes stay local until you run the code. Reset restores the original snippet immediately for another pass.

Live editing

Safe to change before every run.

Fast reset

Return to the original starter instantly.

type-coercion.js
3 linesMonaco Editor
Loading...

Editor Actions

Use the editor to explore the example, then run it to inspect the console.

Console0 entries

Run the code to see output here.

Expected Output

51
6
true

Continue in Playground

Open this concept example in the shared playground to keep experimenting without leaving the broader workspace flow.

Open in Playground