Tutorials › JavaScript Tutorial › JavaScript Data Types

JavaScript Data Types

10 min read · Lesson 5 of 23

Every value in JavaScript has a type. The type decides what you can do with the value: you can multiply numbers, make text uppercase, or check whether something is true. JavaScript has seven primitive types and one structural type, the object.

The primitive types

TypeExample valuesWhat it represents
string"Kisumu", 'M-Pesa'Text
number42, 3.5, -10Whole numbers and decimals
booleantrue, falseYes / no
undefinedundefinedA variable with no value yet
nullnullDeliberately "nothing"
bigint9007199254740993nVery large whole numbers
symbolSymbol("id")A unique identifier

You will use strings, numbers, booleans, undefined and null every day. BigInt and Symbol are specialist tools you can safely skip for now.

Objects

Anything that is not a primitive is an object. Objects group related values together. Arrays and functions are special kinds of objects.

const student = { name: "Amani", county: "Kilifi" }; // object
const counties = ["Kisumu", "Siaya", "Migori"];       // array (an object)

You will study them in Arrays and Objects.

Checking a type with typeof

The typeof operator tells you a value's type as a string.

Example JAVASCRIPT
console.log(typeof "Lamu");
console.log(typeof 2024);
console.log(typeof 9.99);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof { town: "Thika" });
console.log(typeof ["a", "b"]);
console.log(typeof function () {});
Result
string
number
number
boolean
undefined
object
object
function
Try it Yourself »

Notice two things. First, JavaScript has a single number type for both whole numbers and decimals. Second, arrays report "object". To check specifically for an array, use Array.isArray(value).

Warning: typeof null returns "object". This is a famous bug from the very first version of JavaScript that can never be fixed without breaking old websites. To test for null, compare directly: value === null.

Dynamic typing

In JavaScript the variable has no fixed type – only the value inside it does. The same let variable can hold a number now and a string later.

Example JAVASCRIPT
let data = 100;
console.log(typeof data);

data = "one hundred";
console.log(typeof data);
Result
number
string
Try it Yourself »

This flexibility is convenient but can hide mistakes. Good practice is to keep each variable holding one kind of value.

undefined vs null

Both mean "no value", but they are used differently:

  • undefined is what JavaScript gives you automatically – a variable that was never assigned, a missing object property, or a function that returns nothing.
  • null is what you assign on purpose to say "this is empty".
Example JAVASCRIPT
let middleName;
const spouse = null;

console.log(middleName);
console.log(spouse);
Result
undefined
null
Try it Yourself »

Primitives are copied, objects are shared

When you copy a primitive into another variable, you get an independent copy. When you copy an object, both variables point to the same object.

Example JAVASCRIPT
let a = 10;
let b = a;
b = 20;
console.log(a, b);

const shopA = { stock: 10 };
const shopB = shopA;
shopB.stock = 0;
console.log(shopA.stock);
Result
10 20
0
Try it Yourself »

Changing shopB also changed shopA, because they are two names for one object. This difference trips up many beginners, so keep it in mind when you reach the Objects lesson.

Converting between types

Values often arrive as strings – for example, everything a user types into a form. Convert them before doing maths:

Example JAVASCRIPT
const typed = "250";
console.log(typed + 50);
console.log(Number(typed) + 50);
console.log(String(99) + " bob");
console.log(Boolean(0), Boolean("hi"));
Result
25050
300
99 bob
false true
Try it Yourself »

Number(), String() and Boolean() convert explicitly. The first line shows the danger of forgetting: the + joined the text instead of adding.

Tip: When converting user input, Number("abc") gives NaN ("Not a Number"). The Numbers lesson shows how to check for it.
Exercise

Complete the code to print the type of the value.

console.log( 3.14);
Exercise

Convert the string to a number before adding.

const total = ("120") + 30;
Sign in or create a free account to track progress, earn XP, badges and a certificate.