JavaScript Data Types

Every value in JavaScript has a data type. JavaScript is dynamically typed, which means a variable can hold any type, and the type can change.


Primitive types

TypeExample
String"Hello", 'Hi', `Hey`
Number42, 3.14, -7
Booleantrue, false
UndefinedA variable with no value yet
NullAn empty value on purpose
BigInt12345678901234567890n
SymbolA unique identifier (advanced)

Objects

Everything that is not a primitive is an object: arrays, functions, dates and plain objects.

let name = "Robin";          // string
let age = 25;                // number
let isStudent = true;        // boolean
let city;                    // undefined
let empty = null;            // null
let colors = ["red", "green"];          // object (array)
let user = { name: "Robin", age: 25 };  // object

The typeof operator

console.log(typeof "Hello");
console.log(typeof 42);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof null);
console.log(typeof [1, 2]);
console.log(typeof { a: 1 });
console.log(typeof function () {});
console.log(typeof 10n);
Output of the above program is

string
number
boolean
undefined
object
object
object
function
bigint

Notice two surprises. typeof null gives "object", which is an old bug of the language. And an array also gives "object". To check for an array use Array.isArray().

console.log(Array.isArray([1, 2]));
console.log(Array.isArray({ a: 1 }));
Output of the above program is

true
false


undefined and null

  • undefined means “no value has been given yet”.
  • null means “there is no value”, set by the programmer.
Note: JavaScript has only one number type for both integers and decimals. Learn more in Numbers and Math.

Summary

  • Primitive types: string, number, boolean, undefined, null, bigint, symbol.
  • Arrays, functions and objects are objects.
  • Use typeof to check a type, and Array.isArray() for arrays.
  • Next: JavaScript Type Conversion.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Write a JavaScript program to add two numbers
Write a JavaScript program to swap two variables
Interview Questions & Answers
What are the data types in JavaScript?
What is the difference between == and === in JavaScript?
What is NaN in JavaScript?
What is the difference between null and undefined?
What is scope in JavaScript?
What is the difference between var, let and const?
What is hoisting in JavaScript?