🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this chapter, we will explore the reverse() method for arrays in TypeScript. This method is a built-in function that reverses the elements of an array in place. The first array element becomes the last, and the last array element becomes the first. Understanding how to use reverse() is useful for tasks that require the order of elements to be reversed.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The reverse() method reverses the elements of an array in place. This method modifies the original array and returns the array with its elements reversed.
2. Syntax
array.reverse();
Parameters
The reverse() method does not take any parameters.
Return Value
The method returns the array with its elements reversed.
3. Examples
Let's look at some examples to understand how reverse() works in TypeScript.
Example 1: Basic Usage
In this example, we use reverse() to reverse the elements of an array of numbers.
let numbers: number[] = [1, 2, 3, 4, 5];
let reversedNumbers = numbers.reverse();
console.log(reversedNumbers); // Output: [5, 4, 3, 2, 1]
console.log(numbers); // Output: [5, 4, 3, 2, 1]
Example 2: Reversing a String Array
In this example, we use reverse() to reverse the elements of an array of strings.
let fruits: string[] = ["apple", "banana", "mango"];
let reversedFruits = fruits.reverse();
console.log(reversedFruits); // Output: ["mango", "banana", "apple"]
console.log(fruits); // Output: ["mango", "banana", "apple"]
Example 3: Using reverse() with Mixed Data Types
In this example, we use reverse() to reverse the elements of an array containing different data types.
let mixedArray: (number | string | boolean)[] = [1, "apple", true, 42, "banana"];
let reversedMixedArray = mixedArray.reverse();
console.log(reversedMixedArray); // Output: ["banana", 42, true, "apple", 1]
console.log(mixedArray); // Output: ["banana", 42, true, "apple", 1]
Example 4: Reversing an Array of Objects
In this example, we use reverse() to reverse the elements of an array of objects.
interface Person {
name: string;
age: number;
}
let people: Person[] = [
{ name: "Ravi", age: 25 },
{ name: "Ankit", age: 30 },
{ name: "Priya", age: 28 }
];
let reversedPeople = people.reverse();
console.log(reversedPeople);
// Output:
// [ { name: 'Priya', age: 28 },
// { name: 'Ankit', age: 30 },
// { name: 'Ravi', age: 25 } ]
console.log(people);
// Output:
// [ { name: 'Priya', age: 28 },
// { name: 'Ankit', age: 30 },
// { name: 'Ravi', age: 25 } ]
Example 5: Reversing an Empty Array
In this example, we use reverse() on an empty array to see how it behaves.
let emptyArray: number[] = [];
let reversedEmptyArray = emptyArray.reverse();
console.log(reversedEmptyArray); // Output: []
console.log(emptyArray); // Output: []
4. Conclusion
In this chapter, we explored the reverse() method for arrays in TypeScript, which is used to reverse the elements of an array in place. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use reverse() effectively can help in various array manipulation tasks in TypeScript, especially when the order of elements needs to be reversed.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment