Every value in JavaScript has a type.
Primitive types
| Type | Example |
|---|---|
string |
'Hello', "Hi" |
number |
42, 3.14 |
boolean |
true, false |
undefined |
a variable with no value yet |
null |
an intentional "empty" value |
bigint |
9007199254740993n |
symbol |
a unique identifier |
Objects
Objects { }, arrays [ ] and functions are object types.
typeof
typeof tells you the type: typeof 42 gives "number".
JavaScript is dynamically typed: the same variable can hold a number now and a string later.
Example
Example — Checking types with typeof
HTML
<pre id="out"></pre>
<script>
const values = ['Hello', 42, true, undefined, null, [1, 2], { a: 1 }];
let text = '';
for (const v of values) {
text += JSON.stringify(v) + ' → ' + typeof v + '\n';
}
document.getElementById('out').textContent = text;
</script>
Note: typeof null is "object" — a famous old JavaScript quirk.
Try it Now »