Data Types at a Glance

Updated · 6 min read
easyES2026javascriptdata-typestypeofprimitivesinterview

A data type is the kind of a value: text, a number, true or false, a list, and so on. The type decides what you can do with the value: you can multiply numbers, make text uppercase, or look up a property on an object. JavaScript has eight types: seven primitive types and objects. This lesson introduces all eight; later lessons go deep into each.

In One Minute

  • Primitives: string, number, bigint, boolean, undefined, null, symbol. They are simple, single values that can't be changed.
  • Objects: everything else, including arrays, functions, dates and maps. They hold collections of values.
  • typeof value tells you a value's type as a string. Two quirks: typeof null is "object", and typeof a function is "function".
  • JavaScript is dynamically typed: a variable has no fixed type, its current value does.

The Eight Types

TypeExample valuesWhat it is for
string"Navika", 'Dallas', `Grade ${9}`Text
number120, 45.5, -3, NaN, InfinityAll ordinary numbers, whole or decimal
bigint9007199254740993nWhole numbers too large for number
booleantrue, falseYes or no
undefinedundefined"No value was given"
nullnull"Deliberately empty"
symbolSymbol("id")Unique keys for object properties
object{ name: "Advitha" }, [1, 2], new Date()Collections of values and behavior

The first seven are primitives: a primitive value is a single, immutable value. Any operation on it, such as making a string uppercase, creates a new value instead of changing the old one. Objects are collections of properties that can change, and a variable holds a reference to the object rather than the object itself. The difference between the two is the subject of a whole lesson later; for now, know which is which.

The typeof Operator

typeof is an operator that returns a string naming the type of a value.

Syntax

syntax.js
typeof value

Example

typeof.js▶ Try it
console.log(typeof "Navika");
console.log(typeof 120);
console.log(typeof 120n);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof Symbol("id"));
console.log(typeof { grade: 9 });
console.log(typeof [120, 80]);
console.log(typeof null);
console.log(typeof function () {});
Output
string
number
bigint
boolean
undefined
symbol
object
object
object
function

Explanation: most results match the table. Three need a note:

  • Arrays are objects, so typeof [120, 80] is "object". To check for an array, use Array.isArray(value).
  • typeof null is "object". This is a bug from the first version of JavaScript in 1995 that can never be fixed, because fixing it would break existing websites. Check for null with value === null.
  • Functions are objects too, but typeof gives them their own answer, "function", because being callable is so useful to test for.

Dynamic Typing

In Java or C#, you declare a variable's type and it can only hold that type. In JavaScript, variables have no type; values do. The same let variable can hold a number now and a string later:

dynamic.js▶ Try it
let value = 120;
console.log(typeof value, value);
 
value = "one hundred twenty";
console.log(typeof value, value);
 
value = null;
console.log(typeof value, value);
Output
number 120
string one hundred twenty
object null

This flexibility makes small scripts quick to write. It also lets bugs slip through: nothing stops a fee from accidentally becoming a string. Larger projects add checks, tests, or TypeScript (which adds types on top of JavaScript) to catch those mistakes; each is covered later in the course.

undefined and null

Both mean "no value", with a difference in who decided:

undefinednull
MeaningNo value has been given yetIntentionally empty
Who sets itJavaScript, automaticallyYou, on purpose
Typical sourceA declared but unassigned variable, a missing property, a function with no return"This student has no discount"
typeof"undefined""object" (the old bug)
empty.js▶ Try it
let discount;
const student = { name: "Srihitha", scholarship: null };
 
console.log(discount);
console.log(student.scholarship);
console.log(student.phone);
Output
undefined
null
undefined

Explanation: discount was declared without a value, so JavaScript gives it undefined. scholarship was set to null on purpose: Srihitha has no scholarship, and we know it. phone doesn't exist on the object at all, so reading it also gives undefined.

In Practice

The Fee Tracker's first record, with one value of each common type, and a check that a fee really is a number before using it:

record.js▶ Try it
const studentName = "Advitha Anamaneni";  // string
const grade = 9;                           // number
const isPaid = false;                      // boolean
const discountCode = null;                 // null: no code, on purpose
const fees = [120, 80];                    // object (an array)
 
console.log(studentName, grade, isPaid, discountCode, fees);
console.log("Array?", Array.isArray(fees));
 
const total = fees[0] + fees[1];
if (typeof total === "number") {
  console.log("Total due:", total);
}
Output
Advitha Anamaneni 9 false null [120, 80]
Array? true
Total due: 200

Common Mistakes

Testing for null with typeof. typeof value === "null" is never true. Use value === null.

Testing for an array with typeof. It says "object". Use Array.isArray(value).

Numbers stored as strings. Values from forms and URLs arrive as strings: "120" + 30 is "12030", not 150. Convert first with Number("120"); the numbers chapter covers conversion.

Comparing typeof with a bare word. typeof fee === number looks for a variable named number and throws a ReferenceError. Use quotes.

Interview Corner

What are the data types in JavaScript?

Seven primitives (string, number, bigint, boolean, undefined, null, symbol) and objects. Arrays, functions, dates, maps and sets are all objects. Primitives are immutable and compared by value; objects are mutable and variables hold references to them.

Why does typeof null return "object"?

It's a bug from the first implementation, where values carried a type tag and null used the same tag as objects. It was never fixed because changing it would break existing code. Test for null with === null.

What is the difference between undefined and null?

undefined means a value was never assigned: unassigned variables, missing properties and functions without return produce it. null is an explicit "no value" that a programmer sets. null == undefined is true, but null === undefined is false.

Practice

Challenge: Write a function describe(value) that returns "null" for null, "array" for arrays, and otherwise the typeof result. Test it on null, [1], "hi" and 42.

Show solution
describe.js▶ Try it
function describe(value) {
  if (value === null) return "null";
  if (Array.isArray(value)) return "array";
  return typeof value;
}
 
console.log(describe(null));
console.log(describe([1]));
console.log(describe("hi"));
console.log(describe(42));
Output
null
array
string
number

Quick check: Data types

3 questions
1What does typeof [1, 2, 3] return?
2Which value means deliberately empty, set by the programmer?
3How many primitive types does JavaScript have?

Key Takeaways

CheckUse
Is it a string, number, boolean?typeof value === "string"
Is it null?value === null
Is it an array?Array.isArray(value)
Is it a function?typeof value === "function"

Was this tutorial helpful?

Lesson 7 of 139