Surya JS Journey
Learn JavaScript through concepts, code, and guided practice.
A focused workspace for concept study, interactive examples, visual explanations, and challenge solving.
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.
Editor Actions
Use the editor to explore the example, then run it to inspect the console.
Run the code to see output here.
Expected Output
51
6
trueContinue in Playground
Open this concept example in the shared playground to keep experimenting without leaving the broader workspace flow.
Open in PlaygroundRelated Concepts
Data Types
JavaScript has primitive and reference data types, and knowing the difference affects comparison, copying, and mutation.
Equality
JavaScript has loose and strict equality, and they behave differently because loose equality allows coercion.
Truthy and Falsy
JavaScript treats some values as truthy and some as falsy when evaluating conditions.