What data types exist in JavaScript?

👨‍💻 Frontend Developer 🟡 Often Asked 🎚️ Easy
#JavaScript #JS Basics

Brief Answer

JavaScript has 8 data types:

Primitive types (7):

  • number — numbers
  • string — strings
  • boolean — logical type
  • undefined — undefined value
  • null — empty value
  • symbol — unique identifier (ES6)
  • bigint — large integers (ES2020)

Reference type (1):

  • object — objects (including arrays, functions, dates, etc.)

Primitive Data Types

1. Number

Includes integers, decimals, special values.

let age = 25;           // integer
let price = 99.99;      // decimal
let infinity = Infinity; // infinity
let notANumber = NaN;   // "not a number"
 
console.log(typeof age);      // "number"
console.log(typeof infinity); // "number"
console.log(typeof notANumber); // "number"

Features:

  • Maximum safe integer: Number.MAX_SAFE_INTEGER (2^53 - 1)
  • NaN is not equal to itself: NaN === NaNfalse
  • Number checking: Number.isNaN(), Number.isFinite()

2. String

Sequence of characters in quotes.

let name = "Aleksandr";        // double quotes
let surname = 'Ermolov';       // single quotes
let fullName = `${name} ${surname}`; // template strings (ES6)
 
console.log(typeof name);     // "string"
console.log(fullName);        // "Aleksandr Ermolov"

Features:

  • Strings are immutable
  • Character access: name[0] or name.charAt(0)
  • String length: name.length

3. Boolean

Only two values: true or false.

let isActive = true;
let isCompleted = false;
let isAdult = age >= 18;  // comparison result
 
console.log(typeof isActive); // "boolean"

Boolean conversion:

// Falsy values (convert to false):
Boolean(0);         // false
Boolean("");        // false
Boolean(null);      // false
Boolean(undefined); // false
Boolean(NaN);       // false
Boolean(false);     // false
 
// Everything else is truthy (converts to true)
Boolean(1);         // true
Boolean("hello");   // true
Boolean([]);        // true
Boolean({});        // true

4. Undefined

Value of a variable that is declared but not initialized.

let x;
console.log(x);        // undefined
console.log(typeof x); // "undefined"
 
// Function without return also returns undefined
function test() {
  // no return
}
console.log(test()); // undefined

5. Null

Represents intentional absence of value.

let data = null;
console.log(data);        // null
console.log(typeof data); // "object" (this is a bug in JS!)
 
// Correct null check
console.log(data === null); // true

Difference between null and undefined:

let a;           // undefined (not initialized)
let b = null;    // null (intentionally empty)
 
console.log(a == b);  // true (loose comparison)
console.log(a === b); // false (strict comparison)

6. Symbol

Unique identifier introduced in ES6.

let id1 = Symbol("id");
let id2 = Symbol("id");
 
console.log(id1 === id2);    // false (always unique)
console.log(typeof id1);     // "symbol"
 
// Using in objects
let user = {
  name: "Aleksandr",
  [id1]: 123  // symbol as key
};
 
console.log(user[id1]); // 123

7. BigInt

For working with numbers larger than Number.MAX_SAFE_INTEGER.

let bigNumber = 123456789012345678901234567890n; // suffix 'n'
let anotherBig = BigInt("123456789012345678901234567890");
 
console.log(typeof bigNumber); // "bigint"
 
// Cannot mix with regular numbers
// console.log(bigNumber + 1); // TypeError!
console.log(bigNumber + 1n);   // works

Reference Data Type

Object

Everything that is not a primitive is an object.

// Regular object
let person = {
  name: "Aleksandr",
  age: 30
};
 
// Array (also an object!)
let numbers = [1, 2, 3, 4, 5];
 
// Function (also an object!)
function greet() {
  return "Hello!";
}
 
// Date (object)
let now = new Date();
 
console.log(typeof person);  // "object"
console.log(typeof numbers); // "object"
console.log(typeof greet);   // "function" (special case)
console.log(typeof now);     // "object"

Difference between primitives and objects:

// Primitives are passed by value
let a = 5;
let b = a;
a = 10;
console.log(b); // 5 (unchanged)
 
// Objects are passed by reference
let obj1 = { x: 5 };
let obj2 = obj1;
obj1.x = 10;
console.log(obj2.x); // 10 (changed!)

Type Checking

Data TypetypeofCheck Example
number”number”typeof 42
string”string”typeof "hello"
boolean”boolean”typeof true
undefined”undefined”typeof undefined
symbol”symbol”typeof Symbol()
bigint”bigint”typeof 123n
function”function”typeof function(){}
object”object”typeof {}, typeof []
null”object”value === null

More precise checking methods:

// Array check
Array.isArray([1, 2, 3]);        // true
Array.isArray({});               // false
 
// Null check
value === null;                  // true for null
 
// NaN check
Number.isNaN(NaN);              // true
isNaN("hello");                 // true (but imprecise!)
 
// Universal type check
Object.prototype.toString.call([]); // "[object Array]"
Object.prototype.toString.call({}); // "[object Object]"

Practice Tasks

Task 1

console.log(typeof null);
Answer "object" — this is a historical bug in JavaScript that cannot be fixed due to backward compatibility.

Task 2

console.log(typeof NaN);
Answer "number" — `NaN` is technically a numeric type, although it means "not a number".

Task 3

let a = [];
let b = {};
let c = function() {};
 
console.log(typeof a, typeof b, typeof c);
Answer "object object function" — arrays and objects have type "object", functions have "function".

Task 4

console.log(0 == false);
console.log("" == false);
console.log(null == undefined);
Answer `true true true` — loose comparison performs type coercion.

Task 5

let x = Symbol("test");
let y = Symbol("test");
console.log(x === y);
Answer `false` — each Symbol is unique, even with the same description.

Task 6

let big = 9007199254740991n;
console.log(typeof big);
console.log(big + 1);
Answer "bigint" and TypeError — cannot add BigInt with regular numbers without explicit conversion.

Practical Tips

1. Use strict comparison

// Bad
if (value == null) { /* ... */ }
 
// Good
if (value === null || value === undefined) { /* ... */ }
// or
if (value == null) { /* only for null/undefined check */ }

2. Check types correctly

// For arrays
if (Array.isArray(value)) { /* ... */ }
 
// For null
if (value === null) { /* ... */ }
 
// For objects (excluding null and arrays)
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
  /* ... */
}

3. Be careful with type coercion

// Unexpected results
console.log("5" + 3);    // "53" (concatenation)
console.log("5" - 3);    // 2 (subtraction)
console.log(+"5");       // 5 (unary plus)
console.log(!!"hello");  // true (double negation)

Summary

  • JavaScript has 8 data types: 7 primitive + objects
  • Primitives are immutable and passed by value
  • Objects are mutable and passed by reference
  • typeof null === "object" is a bug, use === null
  • Use specialized methods for precise type checking
  • Avoid implicit type coercion — use strict comparison

Knowing data types is the foundation of JavaScript, without which it’s impossible to write quality code!


Want more articles on interview preparation?
Follow EasyAdvice, bookmark the site, and level up every day 💪