JavaScript Data Types
Learn JavaScript data types with typeof: number, string, boolean, null, undefined, symbol, bigint and objects. Clear explanation of each and the output.
1// Primitive Data Types2console.log("=== Primitive Types ===");34// Number5let age = 25;6let price = 99.99;7console.log("Number:", age, typeof age);8console.log("Float:", price, typeof price);910// String11let name = "John";12let message = 'Hello World';13let template = `Name: ${name}`;14console.log("String:", name, typeof name);15console.log("Template Literal:", template);1617// Boolean18let isActive = true;19let isComplete = false;20console.log("Boolean:", isActive, typeof isActive);2122// Undefined23let undefinedVar;24console.log("Undefined:", undefinedVar, typeof undefinedVar);2526// Null27let nullVar = null;28console.log("Null:", nullVar, typeof nullVar);2930// Symbol (ES6)31let sym = Symbol("id");32console.log("Symbol:", sym, typeof sym);3334// BigInt (ES2020)35let bigNumber = 9007199254740991n;36console.log("BigInt:", bigNumber, typeof bigNumber);3738// Reference Types39console.log("\n=== Reference Types ===");4041// Object42let person = {43 name: "John",44 age: 2545};46console.log("Object:", person, typeof person);4748// Array49let numbers = [1, 2, 3, 4, 5];50console.log("Array:", numbers, typeof numbers);5152// Function53function greet() {54 return "Hello";55}56console.log("Function:", greet, typeof greet);
Sample Output
Null: null object
typeof null is "object". This is a known language bug, not a mistake in the code.
Array: [ 1, 2, 3, 4, 5 ] object
Arrays are a kind of object, so typeof cannot tell them apart.
In this one
- The seven primitive types in JavaScript
- How typeof reports a type
- Why typeof null is "object"
- How to tell arrays from objects
- How to test for NaN correctly
How it works
- 1
Number, string and boolean variables are created and printed with typeof.
- 2
An empty let shows undefined.
- 3
null prints "object" because of the old bug.
- 4
Symbol and BigInt values are printed.
- 5
An object, an array and a function are printed with their types.
Edge cases
Checking for null with typeof
typeof null === "null" is always false.
typeof gives "object" for null. Compare directly with value === null.
Arrays look like objects
typeof [1, 2] returns "object".
Use Array.isArray(value) when you need to know if something is an array.
Testing NaN
NaN === NaN is false.
NaN is never equal to anything, even itself. Use Number.isNaN(value).
What's going on
Every value in JavaScript has a type. The type tells you what kind of value it is and what you can do with it. This program creates one example of each type and prints it with typeof. typeof is an operator that returns the type name as a string.
There are seven primitive types. A primitive is a simple single value. They are number, string, boolean, undefined, null, symbol and bigint. JavaScript has only one number type, so 25 and 99.99 are both "number". A string is text. A boolean is true or false. A variable you declare without a value is undefined.
null means "no value on purpose". Here is the surprise: typeof null returns "object". This is a bug from the first version of JavaScript in 1995. It was never fixed because old websites depend on it. To check for null, write value === null.
Symbol creates a unique ID. BigInt holds whole numbers larger than normal numbers can store safely. You write it with an n at the end, like 9007199254740991n.
Objects, arrays and functions are reference types. typeof says "object" for both objects and arrays. Use Array.isArray() to tell them apart. Functions get their own answer, "function".
One more trap: typeof NaN is "number". NaN is not equal to itself, so NaN === NaN is false. Use Number.isNaN(value) to test it.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does typeof null return?
Pick an answer to check it.
Question 2
What is typeof [1, 2, 3]?
Pick an answer to check it.
Question 3
Which value has type "boolean"?
Pick an answer to check it.
Questions people ask
Why is there only one number type?
Ordinary numbers share one binary floating-point type, called Number (IEEE 754 binary64). It stores values using powers of two, so some decimal fractions are approximate. Whole numbers and decimals both use this type.
When should I use BigInt?
When whole numbers go above 9007199254740991, such as large IDs. You cannot mix BigInt and normal numbers in math directly.
What is the difference between null and undefined?
undefined means a value was never set. null means you set it to "nothing" on purpose.
Why does the output show [Function: greet]?
That is how Node prints a function. A browser may show the function code instead.