🗂️ Different Data Types in JavaScript
JavaScript has two different types of values:
Primitive values
Reference values
Primitive values are atomic pieces of data while Reference values are objects that might consist of multiple values.
JavaScript has the Primitive data types:
undefinednullbooleannumberstringsymbolbigint
JavaScript has Reference data types:
ObjectArrayFunctionDateMapSet
and etc.
✅ Primitive Data Type
🎗️ The undefined type
The undefined type is a primitive type that has only one value undefined. By default, when a variable is declared but not initialized, it defaults to undefined.
Consider the following example:
let counter;
console.log(counter); // undefinedIn this example, the counter is a variable. Since counter hasn’t been initialized, it is assigned the value undefined .
⚡️ The null type
The null type is the second primitive data type that also has only one value null. For example:
let obj = null;
console.log(obj); // objectJavaScript defines that null is equal to undefined as follows:
console.log(null == undefined); // true♦️ The number type
JavaScript uses the number type to represent both integer and floating-point numbers.
The following statement declares a variable and initializes its value with an integer:
let num = 100;To represent a floating-point number, you include a decimal point followed by at least one number. For example:
let price = 12.5;
let discount = 0.05;❇️ NaN
NaN stands for Not a Number. It is a special numeric value that indicates an invalid number.
For example, the division of a string by a number returns NaN:
console.log('a'/2); // NaNThe NaN has two special characteristics:
Any operation with
NaNreturnsNaN.The
NaNdoes not equal any value, including itself.
console.log(NaN/2); // NaN
console.log(NaN == NaN); // false💠 The string type
In JavaScript, a string is a sequence of zero or more characters. A string literal begins and ends with either a single quote(') or a double quote (").
A string that begins with a double quote must end with a double quote.
let message = "Bye";A string that begins with a single quote must also end with a single quote.
let greeting = 'Good Morning';If you want to use single quotes or double quotes in a literal string, you need to use the backslash to escape them.
let message = 'I\\'m also a valid string';
// use \\ to escape the single quote (')JavaScript strings are immutable, meaning that they cannot be modified once created. However, you can create a new string from an existing one. For example:
let str = 'JavaScript';
str = str + ' String';🔰The boolean type
The boolean type has two literal values: true and false in lowercase. The following example declares two variables that hold the boolean values.
let inProgress = true;
let completed = false;JavaScript allows values of other types to be converted into boolean values of
trueorfalse.To convert values of other types into boolean values, you use the
Boolean()function.
The following table displays the conversion rules:
Type | true | false | Example |
string | non-empty string | empty string |
|
number | non-zero number and Infinity | 0, NaN |
|
object | non-null object | null |
|
🏷️ The symbol type
JavaScript introduced a new primitive type in ES6: the symbol. Unlike other primitive types, the symbol type does not have a literal form.
To create a symbol, you call the Symbol function as follows:
let s1 = Symbol();The Symbol function creates a new unique value every time you call it.
console.log(Symbol() == Symbol()); // false🪄 The bigint type
The bigint type represents the whole numbers that are larger than 253 – 1. To form a bigint literal number, you append the letter n at the end of the number:
let pageView = 9007199254740991n;
console.log(typeof(pageView)); // 'bigint'✅ Reference Data Type
♦️ The Object type
In JavaScript, an object is a collection of key-value pairs where keys are strings (or Symbols) and values can be any type.
Creating an object using object literal syntax:
let person = {
name: "Alice",
age: 25,
};Accessing properties:
console.log(person.name); // "Alice"
console.log(person["age"]); // 25Dynamic Properties
You can add or remove properties anytime:
person.city = "New York";
delete person.age;♦️ The Array type
An array is a special type of object used to store ordered collections.
Creating an array:
let colors = ["red", "green", "blue"];Accessing elements by index:
console.log(colors[0]); // "red"Arrays are zero-indexed
The first element has index 0, second is 1, and so on.
colors[1] = "yellow"; // Modifies the second element♦️ The Function type
Functions are first-class objects in JavaScript. They can be stored in variables, passed as arguments, or returned from other functions.
Declaring a function:
function greet(name) {
return "Hello, " + name;
}Storing in a variable:
const sayHi = greet;
console.log(sayHi("Sam")); // "Hello, Sam"Functions can have properties too
greet.description = "Greets a person";♦️ The Date type
JavaScript provides the Date object to handle dates and times.
Creating a new date:
let now = new Date();Creating a specific date:
let birthday = new Date("1998-07-15");Common date methods
now.getFullYear(); // Returns the 4-digit year
now.getMonth(); // Returns month (0-11)
now.getDate(); // Returns day of the month♦️ The Map type
A Map is a collection of key-value pairs where keys can be of any type.
Creating and setting values:
let map = new Map();
map.set("name", "Bob");
map.set(1, "One");Getting and checking keys:
map.get("name"); // "Bob"
map.has(1); // true♦️ The Set type
A Set is a collection of unique values.
Creating a Set and adding values:
let set = new Set();
set.add(1);
set.add(2);
set.add(1); // Duplicate, will be ignoredChecking and deleting:
set.has(2); // true
set.delete(1); // removes 1<div class="infographic-jstypes">
<style>
.infographic-jstypes {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--accent2: #1e3a8a;
--accent2-soft:#dbe4fb;
--line: #ddd6c9;
--display-font: 'Fraunces', Georgia, serif;
--body-font: 'Inter', -apple-system, sans-serif;
--mono-font: 'IBM Plex Mono', 'Courier New', monospace;
margin: 0 auto;
box-sizing: border-box;
font-family: var(--body-font);
color: var(--ink);
background: var(--paper);
}
.infographic-jstypes[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--accent2: #9db4f0;
--accent2-soft:#232b47;
--line: #2c313d;
}
.infographic-jstypes * { box-sizing: border-box; }
.infographic-jstypes .ig-inner { padding: 1.75rem clamp(1.25rem, 5vw, 3rem) 2rem; }
.infographic-jstypes .ig-title {
font-family: var(--display-font); font-size: clamp(1.4rem, 4.5vw, 1.9rem);
font-weight: 600; text-align: center; margin: 0 0 1.5rem;
}
.infographic-jstypes .cols {
display: grid; grid-template-columns: 1fr 1fr; gap: 1.25rem;
}
@media (max-width: 560px) {
.infographic-jstypes .cols { grid-template-columns: 1fr; }
}
.infographic-jstypes .col-head {
display: flex; align-items: baseline; gap: 0.5rem;
margin-bottom: 0.6rem;
}
.infographic-jstypes .col-name { font-weight: 700; font-size: 0.95rem; }
.infographic-jstypes .col-hint { font-size: 0.72rem; color: var(--ink-soft); }
.infographic-jstypes .col-name.primitive { color: var(--accent); }
.infographic-jstypes .col-name.reference { color: var(--accent2); }
.infographic-jstypes .chips { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.infographic-jstypes .chip {
font: inherit; font-size: 0.82rem; font-weight: 600; font-family: var(--mono-font);
border: none; cursor: pointer; padding: 0.4rem 0.8rem; border-radius: 999px;
transition: transform 0.12s ease, filter 0.12s ease;
}
.infographic-jstypes .chip:hover { transform: translateY(-1px); }
.infographic-jstypes .chip.primitive { background: var(--accent-soft); color: var(--accent); }
.infographic-jstypes .chip.reference { background: var(--accent2-soft); color: var(--accent2); }
.infographic-jstypes .chip.is-active.primitive { background: var(--accent); color: var(--paper); }
.infographic-jstypes .chip.is-active.reference { background: var(--accent2); color: var(--paper); }
.infographic-jstypes .stage {
margin-top: 1.75rem; background: var(--paper-alt); border: 1px solid var(--line);
padding: 1.5rem 1rem; display: flex; justify-content: center;
}
.infographic-jstypes .stage svg { width: 100%; max-width: 480px; height: auto; overflow: visible; }
.infographic-jstypes .box-shadow-grp { filter: drop-shadow(0 3px 6px rgba(0,0,0,0.15)); }
.infographic-jstypes .prim-box { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 2; }
.infographic-jstypes .prim-text { font-family: var(--mono-font); font-size: 1.15rem; font-weight: 700; fill: var(--accent); }
.infographic-jstypes .ref-tag { fill: var(--accent2-soft); stroke: var(--accent2); stroke-width: 2; }
.infographic-jstypes .ref-tag-text { font-family: var(--mono-font); font-size: 0.85rem; font-weight: 700; fill: var(--accent2); }
.infographic-jstypes .ref-box { fill: var(--paper); stroke: var(--accent2); stroke-width: 2; stroke-dasharray: 5 4; }
.infographic-jstypes .ref-box-text { font-family: var(--mono-font); font-size: 1rem; font-weight: 700; fill: var(--accent2); }
.infographic-jstypes .ref-arrow { stroke: var(--accent2); stroke-width: 2; fill: none; }
.infographic-jstypes .ref-arrow-head { fill: var(--accent2); }
.infographic-jstypes .caption {
text-align: center; font-size: 0.85rem; color: var(--ink-soft); margin-top: 1rem; min-height: 1.2em;
}
.infographic-jstypes .caption strong { color: var(--ink); }
@media (prefers-reduced-motion: reduce) {
.infographic-jstypes *, .infographic-jstypes *::before, .infographic-jstypes *::after {
animation-duration: 0.001ms !important; transition-duration: 0.001ms !important;
}
}
</style>
<div class="ig-inner">
<h2 class="ig-title">JavaScript Data Types</h2>
<div class="cols">
<div class="col">
<div class="col-head">
<span class="col-name primitive">Primitive</span>
<span class="col-hint">stored directly</span>
</div>
<div class="chips" id="primChips"></div>
</div>
<div class="col">
<div class="col-head">
<span class="col-name reference">Reference</span>
<span class="col-hint">stored by pointer</span>
</div>
<div class="chips" id="refChips"></div>
</div>
</div>
<div class="stage">
<svg viewBox="0 0 480 180" xmlns="http://www.w3.org/2000/svg" id="stageSvg"></svg>
</div>
<p class="caption" id="caption"></p>
</div>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-jstypes')
|| document.querySelector('.infographic-jstypes');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') {
dark = true;
} else if (bodyAttr === 'false') {
dark = false;
} else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
const PRIMITIVES = [
{ id: 'undefined', label: 'undefined', value: 'undefined', caption: 'declared, never assigned' },
{ id: 'null', label: 'null', value: 'null', caption: 'intentionally empty' },
{ id: 'boolean', label: 'boolean', value: 'true', caption: 'true or false' },
{ id: 'number', label: 'number', value: '42', caption: 'integers & floats' },
{ id: 'string', label: 'string', value: '"Hi"', caption: 'text, in quotes' },
{ id: 'symbol', label: 'symbol', value: 'Sym()', caption: 'always unique' },
{ id: 'bigint', label: 'bigint', value: '9007…n', caption: 'huge whole numbers' }
];
const REFERENCES = [
{ id: 'object', label: 'Object', glyph: '{ name: "Sam" }', caption: 'key-value pairs' },
{ id: 'array', label: 'Array', glyph: '[ 1, 2, 3 ]', caption: 'ordered list' },
{ id: 'function', label: 'Function', glyph: 'greet() {}', caption: 'callable, storable' },
{ id: 'date', label: 'Date', glyph: 'Aug 1, 2026', caption: 'a point in time' },
{ id: 'map', label: 'Map', glyph: '{ a→1, b→2 }', caption: 'keyed collection' },
{ id: 'set', label: 'Set', glyph: '{ 1, 2, 3 }', caption: 'unique values only' }
];
const primChipsEl = wrapper.querySelector('#primChips');
const refChipsEl = wrapper.querySelector('#refChips');
const svg = wrapper.querySelector('#stageSvg');
const caption = wrapper.querySelector('#caption');
const allChips = [];
function primitiveSVG(item) {
return `
<g class="box-shadow-grp" transform="translate(140,35)">
<rect class="prim-box" x="0" y="0" width="200" height="90" rx="10"></rect>
<text class="prim-text" x="100" y="52" text-anchor="middle">${item.value}</text>
</g>`;
}
function referenceSVG(item) {
return `
<g class="box-shadow-grp" transform="translate(30,50)">
<rect class="ref-tag" x="0" y="0" width="90" height="55" rx="8"></rect>
<text class="ref-tag-text" x="45" y="33" text-anchor="middle">${item.label}</text>
</g>
<path class="ref-arrow" d="M 128 77 L 195 77" />
<polygon class="ref-arrow-head" points="195,70 210,77 195,84" />
<g class="box-shadow-grp" transform="translate(220,30)">
<rect class="ref-box" x="0" y="0" width="230" height="95" rx="10"></rect>
<text class="ref-box-text" x="115" y="58" text-anchor="middle">${item.glyph}</text>
</g>`;
}
function render(kind, item) {
svg.innerHTML = kind === 'primitive' ? primitiveSVG(item) : referenceSVG(item);
caption.innerHTML = `<strong>${item.label}</strong> — ${item.caption}`;
allChips.forEach(c => c.el.classList.toggle('is-active', c.item.id === item.id));
}
function makeChip(kind, item) {
const btn = document.createElement('button');
btn.className = `chip ${kind}`;
btn.textContent = item.label;
btn.addEventListener('click', () => render(kind, item));
allChips.push({ el: btn, item });
return btn;
}
PRIMITIVES.forEach(item => primChipsEl.appendChild(makeChip('primitive', item)));
REFERENCES.forEach(item => refChipsEl.appendChild(makeChip('reference', item)));
render('primitive', PRIMITIVES[0]);
})();
</script>.demo {
font-family: system-ui, sans-serif;
padding: 16px;
}
.demo h1 {
color: #2563eb;
margin: 0 0 8px;
}
.demo p {
color: #4b5563;
margin: 0;
}document.querySelector('.demo h1')?.addEventListener('click', () => {
alert('Hello from live preview!');
});