JavaScript Data Types
In JavaScript, data types refer to the classification of different types of data such as numbers, strings, objects, etc., that can be used in the code. JavaScript is a dynamically typed language, which means you don’t need to specify the type of a variable when declaring it. JavaScript automatically determines the data type based on the value assigned to the variable.
Types of JavaScript Data Types:
- Primitive Data Types: These are the basic data types that represent a single value.
- Non-Primitive Data Types: Also called reference types, these can hold multiple values and are objects.
Primitive Data Types in JavaScript:
- String: Represents a sequence of characters, enclosed in single or double quotes.
- Number: Represents numeric values, both integers and floating-point numbers.
- BigInt: Represents large integers that cannot be represented by the Number type.
- Boolean: Represents a value of either
true
orfalse
. - undefined: Represents a variable that has not been assigned a value.
- Null: Represents the intentional absence of any value or object.
- Symbol: A unique and immutable primitive value used to create object properties.
Non-Primitive Data Types in JavaScript:
- Object: Represents a collection of key-value pairs, including arrays and functions.
How to Check the Type of a Variable:
JavaScript provides the typeof
operator, which can be used to check the data type of a variable. The result will be a string representing the type of the variable.
Syntax
let age = 25;
console.log(typeof age); // Output: number
Example of Checking Data Types:
This example shows how to check different types using the typeof
operator.
Example 1
let name = "John";
let age = 25;
let isStudent = true;
console.log(typeof name); // Output: string
console.log(typeof age); // Output: number
console.log(typeof isStudent); // Output: boolean
Output:
number
boolean
Primitive vs Non-Primitive Data Types:
- Primitive: Data types that are immutable (cannot be changed) and are passed by value. These include String, Number, Boolean, Undefined, Null, BigInt, and Symbol.
- Non-Primitive: Data types that are mutable (can be changed) and are passed by reference. These include Objects (including arrays and functions).
Example of Object Data Type:
Objects are collections of key-value pairs. An object can represent real-world entities like a car or a person.
Example 2
let person = {
name: "John",
age: 25,
isStudent: true
};
console.log(typeof person); // Output: object
Output:
Type Conversion:
Sometimes, you may want to convert one type of data to another. In JavaScript, you can use String()
, Number()
, and Boolean()
to perform type conversions.
Example 3
let str = "123";
let num = Number(str); // Convert string to number
console.log(typeof num); // Output: number
Output:
Common Operations with JavaScript Data Types:
- String: You can concatenate strings using the
+
operator, and find the length of a string usinglength
. - Number: You can perform arithmetic operations like addition, subtraction, multiplication, and division on numbers.
- Boolean: You can use Boolean values in logical expressions.
- Object: You can access object properties using dot notation or bracket notation.