What Are Data Types?
Data types define the kind of value we store in a variable.
For example, a name is text, age is a number, and a yes-or-no value can be represented using a Boolean.
JavaScript data types are divided into two main categories:
- Primitive Data Types
- Non-Primitive Data Types
1. Primitive Data Types
Primitive data types represent single values. JavaScript has seven primitive data types.
String
Used to store text.
let name = "John";
Number
Used to store integers and decimal numbers.
let age = 22;
let price = 99.5;
Boolean
Used to store true or false.
let isStudent = true;
Undefined
A variable that has been declared but has not been assigned a value has the value undefined.
let result;
console.log(result);
Output: undefined
Null
Used to represent the intentional absence of a value.
let selectedItem = null;
BigInt
Used to represent very large integers beyond the safe integer range of the regular Number type.
let number = 12345678901234567890n;
Symbol
Used to create unique values, often as unique object property keys.
let id = Symbol("id");
2. Non-Primitive Data Types
Non-primitive values are used to work with collections of data and more complex structures. In JavaScript, these values are objects.
Object
An object stores related information as key-value pairs.
let student = {
name: "John",
age: 22
};
Array
An array stores multiple values in an ordered list. Arrays are a special type of object in JavaScript.
let fruits = ["Apple", "Banana", "Mango"];
Function
A function is a reusable block of code that performs a task. Functions are also objects in JavaScript.
function greet() {
console.log("Hello!");
}
Top comments (0)