iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

JS Data Types

JavaScript has a small set of primitive types plus objects for everything else. Variables don't have a type — values do.

Primitive types

TypeExampleNotes
string"hello", 'hi', `tpl ${x}`Immutable text.
number42, 3.14, NaN, Infinity64-bit float. No integer type.
bigint9007199254740993nArbitrary-size integers; n suffix.
booleantrue, false
nullnullIntentional empty value.
undefinedundefinedVariable declared but not assigned.
symbolSymbol("id")Unique identifiers, rarely used in app code.

Everything else is an object

KindExample
Plain object{ name: "Ada", age: 36 }
Array[1, 2, 3]
Functionfunction(){…} or () => {…}
Date, Map, Set, RegExp…Built-in object types.

Check the type at runtime

JS
typeof "hi"         // "string"
typeof 42           // "number"
typeof true         // "boolean"
typeof undefined    // "undefined"
typeof null         // "object"   ← historic bug, but it's the answer
typeof {}           // "object"
typeof []           // "object"   ← arrays count as objects
typeof function(){} // "function"

Array.isArray([])   // true       ← use this to test for arrays
Note: JavaScript is loosely typed. A variable can hold any type and reassign to another. TypeScript adds compile-time type checking on top if you want stricter guarantees.

Example

Example
<!DOCTYPE html>
<html>
<body>

<p id="out"></p>

<script>
document.getElementById("out").textContent = "Hello from JS Data Types!";
</script>

</body>
</html>
Try it Yourself »

Exercise

Reliably check if a value is an array.

if ( .isArray(value)) { /* ... */ }

Test yourself

Q1. `typeof null` returns…
Q2. Which is NOT a primitive type?
Q3. How do you check if a value is an array?

Discussion

Loading…