codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

6 / 65 topics
1JavaScript Introduction2JavaScript console.log() & Output3JavaScript Syntax & Statements4JavaScript Comments5JavaScript Variables (var, let, const)6JavaScript Data Types7JavaScript Operators8JavaScript Type Conversion
Tutorials/JavaScript/Data Types & Type Coercion
🌐JavaScript

Data Types & Type Coercion

Updated 2026-04-21
9 min

Data Types & Type Coercion

JavaScript is dynamically typed - variables can hold any type, and types are checked at runtime, not compile time. Understanding types and coercion prevents subtle bugs.

The 8 Data Types

TypeCategoryExampletypeof
stringPrimitive"hello""string"
numberPrimitive42, 3.14, NaN"number"
bigintPrimitive9007199254740991n"bigint"
booleanPrimitivetrue, false"boolean"
undefinedPrimitiveundefined"undefined"
nullPrimitivenull"object" ⚠️
symbolPrimitiveSymbol("id")"symbol"
objectReference{}, [], function(){}"object"

typeof null

typeof null returns "object" - a famous bug from 1995 never fixed for backward compatibility.

Strings

strings.js
1const single = 'Hello';
2const double = "Hello";
3
4// Template literals (backticks) - most powerful
5const name = "World";
6const greeting = `Hello, ${name}!`; // "Hello, World!"
7
8// Multi-line
9const multiline = `
10Line 1
11Line 2
12`;
13
14console.log("JavaScript".length); // 10

Numbers

numbers.js
1const integer = 42;
2const decimal = 3.14;
3const scientific = 2.5e6; // 2,500,000
4
5// Special values
6console.log(Infinity);
7console.log(NaN);
8console.log(0.1 + 0.2); // 0.30000000000000004
9console.log(0.1 + 0.2 === 0.3); // false!

Floating Point

0.1 + 0.2 !== 0.3 is how IEEE 754 works in every language. For precision, multiply to integers first: (0.1 * 10 + 0.2 * 10) / 10 === 0.3.

Booleans

booleans.js
1const isLoggedIn = true;
2const hasPermission = false;
3console.log(5 > 3); // true
4console.log(10 === 20); // false

undefined vs null

empty-values.js
1// undefined = declared but not assigned
2let x;
3console.log(x); // undefined
4
5// null = intentionally empty
6let user = null;
7console.log(user); // null
8
9console.log(null == undefined); // true (loose)
10console.log(null === undefined); // false (strict)

When to Use Which

Use null to intentionally say "no value". Let undefined happen naturally for unassigned variables.

The typeof Operator

typeof.js
1console.log(typeof "hello"); // "string"
2console.log(typeof 42); // "number"
3console.log(typeof true); // "boolean"
4console.log(typeof undefined); // "undefined"
5console.log(typeof null); // "object" ⚠️
6console.log(typeof {}); // "object"
7console.log(typeof []); // "object"
8console.log(typeof function(){}); // "function"
9
10// Better array check
11console.log(Array.isArray([])); // true

Type Coercion

Coercion is JavaScript automatically converting values between types.

String Coercion

string-coercion.js
1// + with a string converts everything to string
2console.log("5" + 3); // "53"
3console.log("Hello" + 42); // "Hello42"
4console.log("" + true); // "true"
5
6// Explicit
7console.log(String(42)); // "42"

Numeric Coercion

numeric-coercion.js
1// Math operators (except +) convert to numbers
2console.log("6" - 2); // 4
3console.log("6" * "3"); // 18
4console.log(true + 1); // 2
5console.log(null + 5); // 5
6
7// Explicit
8console.log(Number("42")); // 42
9console.log(Number("hello")); // NaN
10console.log(Number(true)); // 1
11console.log(Number("")); // 0
12console.log(Number(undefined)); // NaN

Boolean Coercion (Truthy & Falsy)

truthy-falsy.js
1// The 8 FALSY values:
2Boolean(false); // false
3Boolean(0); // false
4Boolean(""); // false
5Boolean(null); // false
6Boolean(undefined); // false
7Boolean(NaN); // false
8Boolean(-0); // false
9Boolean(0n); // false
10
11// Everything else is TRUTHY:
12Boolean("hello"); // true
13Boolean(42); // true
14Boolean([]); // true ← empty array is truthy!
15Boolean({}); // true ← empty object is truthy!
16Boolean("0"); // true ← string "0" is truthy!

Common Gotcha

Empty arrays [] and objects {} are truthy. Check emptiness with arr.length === 0.

== vs === (Loose vs Strict Equality)

equality.js
1// == performs type coercion
2console.log(5 == "5"); // true
3console.log(0 == false); // true
4console.log("" == 0); // true
5
6// === checks type AND value (no coercion)
7console.log(5 === "5"); // false
8console.log(0 === false); // false
9console.log("" === 0); // false

Always Use ===

Use strict equality (===) everywhere. The only exception: value == null conveniently checks both null and undefined.

Practical Example: Type-Safe Input Validation

validation.js
1function validateAge(input) {
2const age = Number(input);
3
4if (Number.isNaN(age)) return "Enter a valid number";
5if (age < 0 || age > 150) return "Age must be 0-150";
6if (!Number.isInteger(age)) return "Must be a whole number";
7
8return "Valid age: " + age;
9}
10
11console.log(validateAge("25")); // "Valid age: 25"
12console.log(validateAge("abc")); // "Enter a valid number"
13console.log(validateAge("25.5")); // "Must be a whole number"

What's Next?

Now that you understand data types and coercion, let's learn about operators - calculations, comparisons, and logical combinations.


PreviousJavaScript Variables (var, let, const)Next JavaScript Operators

Recommended Gear

JavaScript Variables (var, let, const)JavaScript Operators