Exploring JavaScript Data Types
JavaScript is a dynamic and loosely typed programming language, which means variables can hold values of different types without needing explicit type declarations. Understanding JavaScript data types is essential for writing efficient and bug-free code. In this blog post, we will explore the different data types in JavaScript and how they work.
JavaScript Data Types
JavaScript has two main categories of data types:
- Primitive Data Types
- Non-Primitive (Reference) Data Types
1. Primitive Data Types
Primitive data types are immutable, meaning their values cannot be changed. They are stored directly in memory and compared by value.
a) String
A string represents a sequence of characters and is enclosed in single ('), double ("), or backticks (`).
let str1 = "Hello, World!";
let str2 = 'JavaScript';
let str3 = `Template Literal`;b) Number
JavaScript has only one type of number (floating point), which includes integers and decimals.
let num1 = 42;
let num2 = 3.14;
let num3 = 1e3; // 1000c) Boolean
A boolean represents a logical entity and can have two values: true or false.
let isJavaScriptFun = true;
let isCoffeeCold = false;d) Undefined
A variable that has been declared but not assigned a value is undefined.
let notDefined;
console.log(notDefined); // undefinede) Null
null represents an intentional absence of any value.
let emptyValue = null;
console.log(emptyValue); // nullf) BigInt
BigInt is used to represent very large numbers beyond the safe limit of the Number type.
let bigNumber = 123456789012345678901234567890n;
console.log(bigNumber);g) Symbol
A Symbol is a unique and immutable primitive value, commonly used as object keys.
let symbol1 = Symbol("unique");
let symbol2 = Symbol("unique");
console.log(symbol1 === symbol2); // false2. Non-Primitive (Reference) Data Types
Non-primitive data types are mutable and stored by reference.
a) Object
An Object is a collection of key-value pairs.
let person = {
name: "Anil",
age: 25,
isDeveloper: true
};
console.log(person.name); // Anilb) Array
An Array is an ordered collection of values.
let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[1]); // Bananac) Function
A Function is a reusable block of code that can be executed when called.
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Anil"));d) Date
The Date object is used to work with dates and times.
let currentDate = new Date();
console.log(currentDate);Type Checking in JavaScript
JavaScript provides the typeof operator to check the type of a variable.
console.log(typeof 42); // "number"
console.log(typeof "Hello"); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (a known JavaScript quirk)
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"Conclusion
Understanding JavaScript data types is fundamental to becoming a proficient developer. Primitive types are stored by value, whereas reference types are stored by reference. Knowing how these types work helps in debugging, optimizing performance, and writing cleaner code.
Get Anil Vinnakoti’s stories in your inbox
Join Medium for free to get updates from this writer.
Happy coding!




