Master JavaScript Data Types with Examples: A Complete Guide

Bobby Jul 13, 2026

JavaScript, a versatile language, is not just pivotal for web development but also for server-side and mobile applications. A robust understanding of its data types plays a significant role in effective and efficient coding. JavaScript has six primary data types, each with its unique characteristics and use-cases. Let's dive into an exploration of these data types with examples to gain a clear understanding of each.

JavaScript Literals & Data Types: The Building Blocks of Code 🔢
JavaScript Literals & Data Types: The Building Blocks of Code 🔢

JavaScript is dynamically typed, meaning you don't need to declare the type of a variable before assigning value to it. Instead, the JavaScript engine automatically determines the data type when you assign the value to the variable. Now, let's delve into the six data types: Undefined, Null, Boolean, String, Number, and Object. The first two, Undefined and Null, can be classified as primitives, while the others are primitives or complex data structures.

Data Types in JavaScript
Data Types in JavaScript

Primitive Data Types

Primitive data types are basic data types that are not objects and don't have methods and properties. They are immutable, meaning their values can't be changed. Let's explore the two primitive data types in this category.

Master the complete concept of JavaScript Data Types
Master the complete concept of JavaScript Data Types

Undefined

The Undefined type represents variables that have not been assigned a value. Let's create an example:

an image of a poster with some type of text on it, including numbers and symbols
an image of a poster with some type of text on it, including numbers and symbols

var x;
console.log(typeof x);  // Logs 'undefined'

Here, we declare a variable 'x' without giving it a value. When we log 'typeof x', it returns 'undefined'.

Null

Another primitive data type is Null. It represents nothing, no value, or no object. A variable set to null is essentially empty:

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

var y = null;
console.log(typeof y);  // Logs 'object'

Historically, the typeof operator has a bug and returns 'object' for null in JavaScript. However, null typeof should return 'null' in the next JavaScript versions.

Boolean Data Type

The Boolean data type can have one of two values: true or false. These boolean values are typically the result of comparison operations. Let's take a look:

Data type in Javascript
Data type in Javascript

var z = 5 > 2;
console.log(z);  // Logs true

Here, '5 > 2' returns true (a boolean value), so the variable 'z' is created and assigned the value of true.

String Data Type

Types of Data Type 👍❤️
Types of Data Type 👍❤️
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
Data Types in JavaScript
Data Types in JavaScript
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
🚀 JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
🚀 JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
Data Types
Data Types
DataTypes Javascript
DataTypes Javascript
The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had
a diagram showing the different types of data
a diagram showing the different types of data
an info sheet with different types of web pages
an info sheet with different types of web pages
https://i.pinimg.com/originals/51/f4/e5/51f4e5ea55741d75de148fae01d485c3.jpg
https://i.pinimg.com/originals/51/f4/e5/51f4e5ea55741d75de148fae01d485c3.jpg
JSON Format Cheat Sheet (Beginner → Advanced)
JSON Format Cheat Sheet (Beginner → Advanced)
Javascript Fundamental
Javascript Fundamental
Functions in Javascript
Functions in Javascript
Javascript Cheet cheet
Javascript Cheet cheet
Set & Get HTML Data Attributes In Javascript
Set & Get HTML Data Attributes In Javascript
Java Data Types Explained
Java Data Types Explained
Coding with Parvez
Coding with Parvez
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)

JavaScript uses the String data type to represent text. Strings are arrays of characters enclosed in quotes.

You can declare a string in JavaScript using either single quotes ( ' ') or double quotes ( " " ). Here are a few examples:

var str1 = "Hello";
var str2 = 'World';
console.log(str1 + ' ' + str2);  // Logs "Hello World"

Number Data Type

The Number data type is used to represent numeric values. JavaScript numbers can be whole numbers (integer) or decimal numbers (floating point numbers).

Let's declare an integer and a floating-point number:

var intNum = 10;    // Whole number
var floatNum = 15.5;  // Decimal number
console.log(intNum, floatNum);  // Logs 10 15.5

Object Data Type

The Object data type in JavaScript represents a standalone entity that can enclose a whole bunch of values and functions (methods) about an entity. Objects are more flexible than other types as they can accommodate different data types. Here is an example:

var person = {
    name: "John",
    age: 30,
    isEmployee: true
};
console.log(person);  // Logs { name: 'John', age: 30, isEmployee: true } 

In the above object, 'name', 'age', and 'isEmployee' are properties - each storing a different type of data: string, number, and boolean respectively.

Understanding JavaScript data types is crucial for effective programming in JavaScript. It encompasses a well-rounded concept, liberating us from being bound by specific data usage. With this understanding, we can tackle complex tasks, implement efficient code, and smoothly navigate the JavaScript realm.

But the journey doesn't end here. Exploring the properties and methods of each data type, as well as comparing their performance and bandwidth usage, could be your next exciting phase. So, Happy Coding!