Data Types in JavaScript

Understanding data types is crucial in JavaScript as it determines how values are stored and manipulated in your code. In this comprehensive chapter, we'll explore data types in JavaScript from the ground up, covering basic types to more advanced concepts, with detailed explanations and illustrative examples.

Data Types Table

Data TypeDescription
Primitive
NumberNumeric values (integers, decimals)
StringSequences of characters (text)
BooleanLogical values (true or false)
UndefinedVariable declared but not assigned
NullIntentional absence of a value
BigIntIntegers too large for normal numbers (ES2020+)
Non-Primitive
ObjectUnordered collections of key-value pairs
ArrayOrdered collections of items with indexes
SymbolUnique and immutable identifiers (rarely used directly)

Primitive Data Types: The Basic Elements

JavaScript offers a set of primitive data types that represent fundamental kinds of data:

Number 

Represent numeric values, both integers (whole numbers) and floating-point numbers (decimals). They are stored in 64-bit floating-point format according to the IEEE 754 standard.

				
					let age = 30;
let pi = 3.14159;
console.log(typeof age);  // Output: number
console.log(typeof pi);   // Output: number

				
			

Strings

 Represent sequences of characters, often used for text. Strings are immutable (unchangeable) in JavaScript.

				
					let name = "Alice";
let greeting = 'Hello, world!';
console.log(typeof name);  // Output: string
console.log(typeof greeting); // Output: string

				
			

Booleans

Represent logical values: true or false. They are used to indicate conditions or choices.

				
					let isLoggedIn = true;
let isNightTime = false;
console.log(typeof isLoggedIn);  // Output: boolean
console.log(typeof isNightTime); // Output: boolean

				
			

Undefined

Represents a variable that has been declared but not yet assigned a value.

				
					let uninitialized;
console.log(typeof uninitialized); // Output: undefined

				
			

Null

Represents the intentional absence of a value. It’s different from undefined as it’s explicitly used to indicate that there is no value.

				
					let emptyVariable = null;
console.log(typeof emptyVariable); // Output: object (historical quirk)

				
			

BigInt

BigInt is a new data type introduced in ES2020, allowing for the representation of arbitrarily large integers.

				
					let bigNum = 123456789012345678901234567890n;
console.log(typeof bigNum); // Output: bigint

				
			

Non-Primitive Data Types: Complex Structures

JavaScript also supports non-primitive data types that allow you to group and organize data:

Objects

Unordered collections of key-value pairs. Keys are typically strings, and values can be of any data type.

				
					let person = {
    name: "Bob",
    age: 35,
    skills: ["JavaScript", "Python"]
};
console.log(typeof person); // Output: object

				
			

Arrays

Ordered collections of items, where each item has an index (starting from 0). Elements can be of any data type, and arrays are mutable (changeable).

				
					let colors = ["red", "green", "blue"];
console.log(typeof colors); // Output: object (historical quirk)
console.log(colors[1]);     // Output: green (accessing by index)

				
			

Symbol

Introduced in ECMAScript 6, the symbol data type represents a unique and immutable value that can be used as an identifier for object properties.

				
					let sym = Symbol("description");
console.log(typeof sym); // Output: symbol

				
			

IMPORTANT NOTE

In JavaScript, functions are not considered data types in the same way as numbers, strings, or objects. They are a fundamental building block for creating reusable blocks of code, but they don’t hold data themselves.

The table should focus on data types that represent and store information. So because function is not mention in the Table

Lets see the Function Now

Function

In JavaScript, functions are first-class citizens, meaning they can be assigned to variables, passed as arguments, and returned from other functions.

				
					let add = function(a, b) {
  return a + b;
};

console.log(typeof add); // Output: function

				
			

Understanding data types in JavaScript is fundamental for writing efficient and bug-free code. By knowing the various data types and their characteristics, you can better manage your data, perform operations effectively, and avoid common pitfalls. Whether you're working with basic types like numbers and strings or more complex types like objects and functions, a solid grasp of data types is essential for becoming proficient in JavaScript programming.Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India