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
| Type | Example | Notes |
|---|---|---|
string | "hello", 'hi', `tpl ${x}` | Immutable text. |
number | 42, 3.14, NaN, Infinity | 64-bit float. No integer type. |
bigint | 9007199254740993n | Arbitrary-size integers; n suffix. |
boolean | true, false | — |
null | null | Intentional empty value. |
undefined | undefined | Variable declared but not assigned. |
symbol | Symbol("id") | Unique identifiers, rarely used in app code. |
Everything else is an object
| Kind | Example |
|---|---|
| Plain object | { name: "Ada", age: 36 } |
| Array | [1, 2, 3] |
| Function | function(){…} 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)) { /* ... */ }
A built-in static method on the Array constructor.
Discussion
Loading…