Explain JavaScript Data Types
JavaScript has several data types that are used to represent different kinds of data. These data types can be broadly categorized into primitive types and objects. Here’s an overview of each data type with examples:
1. Primitive Data Types
a. Number
Represents both integer and floating-point numbers.
let age = 25;
let price = 19.99;
console.log(typeof age); // "number"
console.log(typeof price); // "number"
b. String
Represents a sequence of characters.
let greeting = "Hello, World!";
console.log(typeof greeting); // "string"
c. Boolean
Represents a logical entity and can have two values: true
or false
.
let isAvailable = true;
let isClosed = false;
console.log(typeof isAvailable); // "boolean"
console.log(typeof isClosed); // "boolean"
d. Null
Represents the intentional absence of any object value. It is treated as a falsy value.
let emptyValue = null;
console.log(typeof emptyValue); // "object" (this is a known bug in JavaScript, it should be "null")
e. Undefined
A variable that has been declared but not assigned a value.
let notAssigned;
console.log(typeof notAssigned); // "undefined"
f. Symbol
A unique and immutable data type often used to identify object properties uniquely.
let sym = Symbol('unique');
console.log(typeof sym); // "symbol"
g. BigInt
Represents integers with arbitrary precision.
let bigIntNumber = 1234567890123456789012345678901234567890n;
console.log(typeof bigIntNumber); // "bigint"
2. Object Data Type
Used to store collections of data and more complex entities. Objects can contain properties and methods.
a. Object
Represents an instance through which we can access members.
let person = {
name: "John",
age: 30
};
console.log(typeof person); // "object"
b. Array
A special type of object used for storing ordered collections.
let numbers = [1, 2, 3, 4, 5];
console.log(typeof numbers); // "object"
c. Function
A callable object that can be defined using a function declaration or expression.
function greet() {
return "Hello, World!";
}
console.log(typeof greet); // "function"
d. Date
Represents a date and time.
let now = new Date();
console.log(typeof now); // "object"
e. RegExp
Represents a regular expression.
let pattern = /ab+c/;
console.log(typeof pattern); // "object"
Example Output
Here’s an example script that demonstrates the use of various data types and outputs their types:
let age = 25;
let price = 19.99;
let greeting = "Hello, World!";
let isAvailable = true;
let emptyValue = null;
let notAssigned;
let sym = Symbol('unique');
let bigIntNumber = 1234567890123456789012345678901234567890n;
let person = { name: "John", age: 30 };
let numbers = [1, 2, 3, 4, 5];
function greet() { return "Hello, World!"; }
let now = new Date();
let pattern = /ab+c/;
console.log(typeof age); // "number"
console.log(typeof price); // "number"
console.log(typeof greeting); // "string"
console.log(typeof isAvailable); // "boolean"
console.log(typeof emptyValue); // "object"
console.log(typeof notAssigned); // "undefined"
console.log(typeof sym); // "symbol"
console.log(typeof bigIntNumber); // "bigint"
console.log(typeof person); // "object"
console.log(typeof numbers); // "object"
console.log(typeof greet); // "function"
console.log(typeof now); // "object"
console.log(typeof pattern); // "object"
By running this script, you’ll see the type of each variable output to the console, demonstrating the different data types available in JavaScript.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.