Data Types

JavaScript · Lesson 6 of 61 6 min read Updated

The value "250" and the value 250 look almost the same, but JavaScript treats them very differently. Add 50 to the first and you get "25050"; add 50 to the second and you get 300. The difference is their data type, the kind of value they are.

In this lesson you will meet all eight data types in JavaScript, learn to check a value's type with typeof, and convert values from one type to another without surprises.

The eight data types

Every value in JavaScript belongs to one of eight types. Seven of them are primitive types: simple, single values that cannot be changed. The eighth, object, holds collections of values.

TypeWhat it holdsExample
stringText"Kochi", 'Isha'
numberWhole and decimal numbers42, 3.5, -7
bigintVery large whole numbers9007199254740993n
booleanYes or notrue, false
undefinedA variable with no value yetundefined
null"No value", set on purposenull
symbolA unique identifierSymbol("id")
objectCollections and everything else{ name: "Farhan" }, [1, 2, 3]

You will use strings, numbers, booleans, null, undefined and objects every day. BigInt is for numbers larger than about nine quadrillion, which you will see in Numbers & Math. Symbols are used in advanced code and libraries, and you can ignore them for now.

Arrays, functions and dates are not separate types: they are all special kinds of object.

Checking a type with typeof

The typeof operator tells you the type of a value as a string.

JavaScriptRun it yourself
console.log(typeof "Kochi");
console.log(typeof 42);
console.log(typeof 3.5);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof 10n);
console.log(typeof { name: "Farhan" });
console.log(typeof null);
Output
string
number
number
boolean
undefined
bigint
object
object

Notice two things. First, there is only one number type: whole numbers and decimals are both number. Second, typeof null says "object". This is a bug from the very first version of JavaScript that can never be fixed, because fixing it would break old websites. To check for null, compare directly: value === null.

typeof has one more quirk: for an array it also says "object". To check whether a value is an array, use Array.isArray(value). For a function, typeof gives "function", which is handy.

undefined and null

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

  • undefined usually means JavaScript has not been given a value: a variable declared without one, a missing function argument, or an object property that does not exist.
  • null means you, the programmer, decided there is no value, for example a user who has not chosen a delivery slot.
JavaScriptRun it yourself
let deliverySlot;          // not set yet
const middleName = null;   // set on purpose: this person has none

console.log(deliverySlot);
console.log(middleName);
Output
undefined
null

A good habit: never assign undefined yourself. Use null when you want to say "empty on purpose", and let undefined mean "not set". The operators in Nullish Coalescing & Optional Chaining handle both neatly.

Dynamic typing

JavaScript is dynamically typed: a variable does not have a fixed type, only its current value does. The same let variable can hold a number now and a string later. This is flexible, but it also means mistakes are not caught until the code runs, so keep each variable to one type. (TypeScript, a popular language built on JavaScript, adds fixed types for exactly this reason.)

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 names point to the same object.

JavaScriptRun it yourself
let a = 10;
let b = a;
b = 20;
console.log(a, b);

const order1 = { item: "Samosa", qty: 2 };
const order2 = order1;
order2.qty = 5;
console.log(order1.qty, order2.qty);
Output
10 20
5 5

Changing b did not touch a. But order1 and order2 are two names for one object, so a change through either name shows up in both. You will learn how to make real copies of objects in Destructuring, Rest & Spread.

Converting between types

Values often arrive as the wrong type. Text typed into a form field is always a string, even if the user typed digits. Convert it yourself with Number(), String() and Boolean().

JavaScriptRun it yourself
const fromForm = "250"; // form values are always strings

console.log(fromForm + 50);
console.log(Number(fromForm) + 50);
console.log("8" - 3);
console.log(String(99) + 1);
console.log(Number("12 kg"));
console.log(Boolean(""), Boolean("hello"));
Output
25050
300
5
991
NaN
false true

Read each line carefully:

  • + with a string joins text, so "250" + 50 gives "25050".
  • After Number(), it is a real sum: 300.
  • - only works on numbers, so JavaScript quietly converted "8" to 8. This silent conversion is called type coercion.
  • Number("12 kg") cannot make a number and gives NaN, which means "Not a Number".
  • An empty string converts to false; any other string converts to true.

Rely on explicit conversion, not coercion. It makes your intent clear and avoids surprises. More conversion tools, such as parseInt(), are covered in Numbers & Math.

Common mistakes

  • Adding form input without converting. "500" + "250" is "500250". Use Number() first.
  • Checking for null with typeof. It returns "object". Use value === null.
  • Checking for arrays with typeof. It also returns "object". Use Array.isArray().
  • Thinking a copied object is separate. const b = a with an object shares it; it does not copy it.

Key points

  • JavaScript has eight types: string, number, bigint, boolean, undefined, null, symbol and object.
  • typeof returns a value's type as a string; typeof null is "object" for historical reasons.
  • undefined means "not set"; null means "empty on purpose".
  • Variables can change type, but keeping one type per variable avoids bugs.
  • Primitives are copied; objects are shared between variables.
  • Convert types explicitly with Number(), String() and Boolean().

Exercise

A shop form in Jaipur gives you three strings: "3" (quantity), "149" (price) and "yes" (gift wrap). Convert the quantity and price to numbers and print the total. Then print the type of the total with typeof, and print what Number("yes") gives.

Show solution
JavaScript
const quantity = Number("3");
const price = Number("149");
const total = quantity * price;

console.log("Total:", total);
console.log(typeof total);
console.log(Number("yes"));
Output
Total: 447
number
NaN

After conversion the values are numbers, so the multiplication works and the total is a number. Text that is not a number becomes NaN.

Quick Quiz

1. What does typeof null return?

2. What is the result of "20" + 5?

3. How should you check whether a value is an array?

4. After const a = { n: 1 }; const b = a; b.n = 2;, what is a.n?

Learn JavaScript with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call