In this chapter, we will learn about the different data types in JavaScript. Data types specify the kind of data that can be stored and manipulated within a program. JavaScript is a dynamically typed language, meaning you don’t have to specify the data type of a variable when you declare it. We will cover:
- Introduction to Data Types
- Primitive Data Types
- Number
- String
- Boolean
- Undefined
- Null
- Symbol
- BigInt
- Non-Primitive Data Types
- Object
- Array
- Function
- Type Inference in JavaScript
- Examples of Using Different Data Types
Introduction to Data Types
JavaScript variables can hold different types of data. The data type of a variable determines the kind of operations that can be performed on it. JavaScript automatically determines the data type based on the value assigned to the variable.
Primitive Data Types
Primitive data types are the most basic data types in JavaScript. They include:
Number
The Number
data type represents both integer and floating-point numbers.
Example
let age = 25; // Integer let pi = 3.14; // Floating-point let hex = 0xFF; // Hexadecimal let exp = 2.5e6; // Exponential
String
The String
data type is used to represent text. Strings are enclosed in single quotes ('
) or double quotes ("
).
Example
let firstName = "Ramesh"; let greeting = 'Hello, World!';
Boolean
The Boolean
data type represents logical values and can be either true
or false
.
Example
let isStudent = true; let isAdult = false;
Undefined
A variable that has been declared but not assigned a value has the undefined
data type.
Example
let name; console.log(name); // Output: undefined
Null
The Null
data type represents an intentional absence of any object value. It is one of JavaScript’s primitive values.
Example
let person = null; console.log(person); // Output: null
Symbol
The Symbol
data type is used to create unique and immutable identifiers. Each Symbol
value is unique.
Example
let sym1 = Symbol(); let sym2 = Symbol('description');
BigInt
The BigInt
data type is used to represent integers that are too large to be represented by the Number
data type.
Example
let bigNumber = BigInt(1234567890123456789012345678901234567890n);
Non-Primitive Data Types
Non-primitive data types are objects, which are collections of properties and methods.
Object
The Object
data type is used to store collections of data and more complex entities.
Example
let person = { firstName: "Ramesh", lastName: "Fadatare", age: 25 };
Array
The Array
data type is a special type of object used to store ordered collections of data.
Example
let fruits = ["Apple", "Banana", "Mango"];
Function
The Function
data type represents executable code. Functions are objects in JavaScript and can be assigned to variables, passed as arguments, and returned from other functions.
Example
function greet(name) { return "Hello, " + name; } let greeting = greet("Ramesh"); console.log(greeting); // Output: Hello, Ramesh
Type Inference in JavaScript
JavaScript is dynamically typed, meaning you don’t have to specify the data type of a variable when you declare it. The data type is automatically determined based on the value assigned to the variable.
Example
let age = 25; // Number let name = "Ramesh"; // String let isStudent = true; // Boolean
Examples of Using Different Data Types
Example 1: Number and String
let age = 25; let name = "Ramesh"; console.log(name + " is " + age + " years old."); // Output: Ramesh is 25 years old.
Example 2: Boolean
let isAdult = age > 18; if (isAdult) { console.log(name + " is an adult."); // Output: Ramesh is an adult. } else { console.log(name + " is not an adult."); }
Example 3: Undefined and Null
let address; console.log(address); // Output: undefined address = null; console.log(address); // Output: null
Example 4: Object
let person = { firstName: "Ramesh", lastName: "Fadatare", age: 25 }; console.log(person.firstName + " " + person.lastName); // Output: Ramesh Fadatare
Example 5: Array
let fruits = ["Apple", "Banana", "Mango"]; console.log(fruits[0]); // Output: Apple console.log(fruits.length); // Output: 3
Example 6: Function
function multiply(a, b) { return a * b; } let result = multiply(5, 3); console.log(result); // Output: 15
Conclusion
In this chapter, you learned about the different data types in JavaScript, including primitive and non-primitive data types. You also learned that JavaScript is dynamically typed, meaning you don’t have to specify the data type of a variable when you declare it. Understanding these data types is essential for writing and understanding JavaScript code.