Academic Block

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);

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);

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);

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);

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);

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);

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);

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);

Arrays

const colors = ["red", "green", "blue"];

console.log(colors);
console.log(typeof colors);
console.log(Array.isArray(colors));

Functions

function greet() {
    return "Hello";
}

console.log(greet);
console.log(typeof greet);

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);

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);

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);

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");
}

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);

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);

Common Mistakes With Data Types

  • Assuming typeof null returns "null" instead of "object".
  • Using typeof to check for arrays instead of Array.isArray().
  • Relying on implicit type coercion without understanding how it works, leading to unexpected results.
  • Confusing null and undefined.
  • 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 null versus leaving a variable undefined.

Quick Summary

  • JavaScript has seven primitive types: string, number, bigint, boolean, undefined, null, and symbol.
  • Reference types include object, array, and function.
  • The typeof operator 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);
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. Use typeof to check most types, and Array.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.

📝 JavaScript Code
👁 Console Output

Click “Run Code” to see the output here.

Ctrl + Enter to run code Esc to close