JavaScript Data TypesGet Certified
Every value in JavaScript has a type. Understanding data types is fundamental because it affects how values behave, how they are compared, and how they can be converted.
JavaScript has two broad categories of data types: primitive types and reference types (also called non-primitive types).
Primitive Data Types
Primitive values are immutable (they cannot be changed once created) and are stored directly. There are seven primitive types in modern JavaScript.
1. String
A string represents a sequence of characters. Strings can be written with single quotes, double quotes, or backticks (template literals).
let firstName = "Michael";
let lastName = 'Johnson';
let fullName = `Hello, ${firstName}`;
console.log(firstName);
console.log(lastName);
console.log(fullName);
console.log(typeof firstName);
${...}.typeof are printed.2. Number
The number type represents both integers and floating-point
numbers. Special numeric values include Infinity,
-Infinity, and NaN.
let age = 28;
let price = 19.99;
let infinite = Infinity;
let notANumber = NaN;
console.log(age, typeof age);
console.log(price, typeof price);
console.log(infinite, typeof infinite);
console.log(notANumber, typeof notANumber);
number.number.number.3. BigInt
BigInt is used for integers that are too large for the
regular number type. A BigInt is created by appending
n to an integer literal.
let bigNumber = 1234567890123456789012345n;
console.log(bigNumber);
console.log(typeof bigNumber);
n creates a BigInt. It can represent integers of arbitrary size."bigint") are displayed.4. Boolean
A boolean has only two possible values: true or
false. Booleans are commonly used in conditions.
let isLoggedIn = true;
let hasPermission = false;
console.log(isLoggedIn, typeof isLoggedIn);
console.log(hasPermission, typeof hasPermission);
true.false."boolean") are printed.5. Undefined
A variable that has been declared but not assigned a value has the
type undefined.
let notAssigned;
console.log(notAssigned);
console.log(typeof notAssigned);
undefined.undefined.6. Null
null is an intentional absence of any object value. Note
that typeof null returns "object" — this is a
well-known historical quirk of JavaScript.
let emptyValue = null;
console.log(emptyValue);
console.log(typeof emptyValue);
null means “no value on purpose”.null, but typeof reports "object" (a known quirk).7. Symbol
A Symbol is a unique and immutable primitive value, often
used as an object property key to avoid name collisions.
let id = Symbol("id");
let anotherId = Symbol("id");
console.log(id);
console.log(typeof id);
console.log(id === anotherId);
Symbol() call creates a unique value."symbol") are shown.false because the two symbols are different.Reference Types (Objects)
Reference types store a reference (memory address) to the actual data. The most common reference types are objects, arrays, and functions.
Objects
const person = {
name: "Michael",
age: 28
};
console.log(person);
console.log(typeof person);
"object") are displayed.Arrays
const colors = ["red", "green", "blue"];
console.log(colors);
console.log(typeof colors);
console.log(Array.isArray(colors));
typeof reports "object" for arrays.Array.isArray() correctly identifies the value as an array.Functions
function greet() {
return "Hello";
}
console.log(greet);
console.log(typeof greet);
typeof returns "function" for functions.The typeof Operator – Quick Reference
| Value | typeof result |
|---|---|
| “Michael” | string |
| 28 | number |
| 1234567890123456789012345n | bigint |
| true / false | boolean |
| undefined | undefined |
| null | object (quirk) |
| Symbol(“id”) | symbol |
| { } | object |
| [ ] | object |
| function () {} | function |
JavaScript Is Dynamically Typed
Because JavaScript is dynamically typed, a variable can hold different data types at different points in the program.
let value = "Michael";
console.log(typeof value);
value = 28;
console.log(typeof value);
value = true;
console.log(typeof value);
typeof reports "string".typeof reports "number".typeof reports "boolean".Type Conversion
Implicit Type Conversion (Coercion)
JavaScript sometimes automatically converts a value from one type to another. This is called implicit type conversion, or type coercion.
console.log("5" + 2);
console.log("5" - 2);
console.log("5" * "2");
console.log(1 + true);
+ meets a string, the number is converted to a string → "52".- operator forces both operands to numbers → 3.10.true becomes 1, so the result is 2.Explicit Type Conversion (Casting)
You can also convert a value’s type manually using built-in
functions such as String(), Number(),
and Boolean().
let numberValue = 100;
let stringValue = String(numberValue);
let backToNumber = Number(stringValue);
let boolValue = Boolean(1);
console.log(stringValue, typeof stringValue);
console.log(backToNumber, typeof backToNumber);
console.log(boolValue, typeof boolValue);
String() explicitly creates a string.Number() converts the string back to a number.Boolean(1) produces true.Truthy and Falsy Values
When a value is used in a boolean context, such as an
if condition, JavaScript converts it to either
true or false.
The following values are always falsy:
false, 0, "",
null, undefined, and NaN.
Every other value is truthy.
if ("") {
console.log("Truthy");
} else {
console.log("Falsy");
}
if ("Michael") {
console.log("Truthy");
} else {
console.log("Falsy");
}
false in a condition, so the else branch runs.true, so the first branch runs.null vs undefined
Although both represent “no value,” they are used differently.
undefined usually means a variable has not been
assigned a value, while null means a value has been
intentionally set to “nothing.”
let notAssigned;
let intentionallyEmpty = null;
console.log(notAssigned);
console.log(intentionallyEmpty);
console.log(notAssigned == intentionallyEmpty);
console.log(notAssigned === intentionallyEmpty);
undefined.== returns true because both are “empty”.=== returns false because the types differ.Practical Example
Let’s combine several data types together in a small practical example.
const name = "Michael";
const age = 28;
const isDeveloper = true;
const skills = ["JavaScript", "React", "Node.js"];
const address = null;
console.log("Name:", name, typeof name);
console.log("Age:", age, typeof age);
console.log("Developer:", isDeveloper, typeof isDeveloper);
console.log("Skills:", skills, typeof skills);
console.log("Address:", address, typeof address);
null are stored in constants.typeof.Common Mistakes With Data Types
- Assuming
typeof nullreturns"null"instead of"object". - Using
typeofto check for arrays instead ofArray.isArray(). - Relying on implicit type coercion without understanding how it works, leading to unexpected results.
- Confusing
nullandundefined. - Comparing values with
==instead of===and getting unexpected coercion.
Best Practices for Data Types
- Prefer strict equality (
===) over loose equality (==) to avoid unexpected coercion. - Use explicit conversion (
String(),Number(),Boolean()) instead of relying on implicit coercion. - Use
Array.isArray()to check for arrays. - Initialize variables with a sensible default value when possible.
- Be intentional about using
nullversus leaving a variableundefined.
Quick Summary
- JavaScript has seven primitive types:
string,number,bigint,boolean,undefined,null, andsymbol. - Reference types include
object,array, andfunction. - The
typeofoperator returns a string describing a value’s type. - JavaScript is dynamically typed, so a variable can hold different types over time.
- Type coercion can happen implicitly or be done explicitly with functions like
Number(). - Some values are falsy; everything else is truthy.
Try It Yourself
Modify the variables below. Try changing the values, adding a new
variable of a different type, and logging the result of
typeof for each.
let name = "Michael";
let age = 28;
let isDeveloper = true;
console.log(typeof name);
console.log(typeof age);
console.log(typeof isDeveloper);
typeof is used to inspect the type of each variable. Experiment by changing the values or adding new ones.Tip: A simple rule to remember is: primitive types store the value itself, while reference types (objects, arrays, functions) store a reference to the value. Usetypeofto check most types, andArray.isArray()for arrays.
Understanding JavaScript’s data types is essential before moving on to operators, type conversion in depth, functions, objects, arrays, and more advanced JavaScript concepts.
🧪 Test Your JavaScript Code
Edit the code on the left and click “Run Code” to see the console output on the right.
Click “Run Code” to see the output here.
Code Explanation
The complete example stays visible. Use Previous and Next to move through its logical blocks.
Select Next to begin exploring the code.