JavaScript Data Types

typeof null is "object". That is a bug from 1995. Live with it.

BeginnerBasic ProgramsExample 6 of 10
javascript-data-types.js
1// Primitive Data Types
2console.log("=== Primitive Types ===");
3
4// Number
5let age = 25;
6let price = 99.99;
7console.log("Number:", age, typeof age);
8console.log("Float:", price, typeof price);
9
10// String
11let name = "John";
12let message = 'Hello World';
13let template = `Name: ${name}`;
14console.log("String:", name, typeof name);
15console.log("Template Literal:", template);
16
17// Boolean
18let isActive = true;
19let isComplete = false;
20console.log("Boolean:", isActive, typeof isActive);
21
22// Undefined
23let undefinedVar;
24console.log("Undefined:", undefinedVar, typeof undefinedVar);
25
26// Null
27let nullVar = null;
28console.log("Null:", nullVar, typeof nullVar);
29
30// Symbol (ES6)
31let sym = Symbol("id");
32console.log("Symbol:", sym, typeof sym);
33
34// BigInt (ES2020)
35let bigNumber = 9007199254740991n;
36console.log("BigInt:", bigNumber, typeof bigNumber);
37
38// Reference Types
39console.log("\n=== Reference Types ===");
40
41// Object
42let person = {
43 name: "John",
44 age: 25
45};
46console.log("Object:", person, typeof person);
47
48// Array
49let numbers = [1, 2, 3, 4, 5];
50console.log("Array:", numbers, typeof numbers);
51
52// Function
53function greet() {
54 return "Hello";
55}
56console.log("Function:", greet, typeof greet);

Output

=== Primitive Types ===
Number: 25 number
Float: 99.99 number
String: John string
Template Literal: Name: John
Boolean: true boolean
Undefined: undefined undefined
Null: null object
Symbol: Symbol(id) symbol
BigInt: 9007199254740991n bigint
=== Reference Types ===
Object: { name: 'John', age: 25 } object
Array: [ 1, 2, 3, 4, 5 ] object
Function: [Function: greet] function

What's going on

typeof null === "object". Not a joke. Check null with === null.

typeof NaN is "number". Number.isNaN is the test, not === NaN (NaN is not equal to itself).