🎓 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 unshift() method for arrays in TypeScript. This method is a built-in function that adds one or more elements to the beginning of an array and returns the new length of the array. Understanding how to use unshift() is useful for managing and modifying arrays, especially when you need to add elements to the start of the array.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array. This method modifies the original array.
2. Syntax
array.unshift(element1, element2, ..., elementN);
Parameters
element1, element2, ..., elementN: The elements to add to the beginning of the array.
Return Value
The method returns the new length of the array after the elements have been added.
3. Examples
Let's look at some examples to understand how unshift() works in TypeScript.
Example 1: Basic Usage
In this example, we use unshift() to add a single element to the beginning of an array.
let numbers: number[] = [2, 3, 4, 5];
let newLength = numbers.unshift(1);
console.log(newLength); // Output: 5
console.log(numbers); // Output: [1, 2, 3, 4, 5]
Example 2: Adding Multiple Elements
In this example, we use unshift() to add multiple elements to the beginning of an array.
let numbers: number[] = [3, 4, 5];
let newLength = numbers.unshift(1, 2);
console.log(newLength); // Output: 5
console.log(numbers); // Output: [1, 2, 3, 4, 5]
Example 3: Using unshift() with Strings
In this example, we use unshift() to add elements to an array of strings.
let fruits: string[] = ["banana", "mango"];
let newLength = fruits.unshift("apple", "orange");
console.log(newLength); // Output: 4
console.log(fruits); // Output: ["apple", "orange", "banana", "mango"]
Example 4: Using unshift() with Objects
In this example, we use unshift() to add objects to an array of objects.
interface Person {
name: string;
age: number;
}
let people: Person[] = [
{ name: "Ankit", age: 30 },
{ name: "Priya", age: 28 }
];
let newPerson: Person = { name: "Ravi", age: 25 };
let anotherPerson: Person = { name: "John", age: 22 };
let newLength = people.unshift(newPerson, anotherPerson);
console.log(newLength); // Output: 4
console.log(people);
// Output:
// [ { name: 'Ravi', age: 25 },
// { name: 'John', age: 22 },
// { name: 'Ankit', age: 30 },
// { name: 'Priya', age: 28 } ]
Example 5: Using unshift() in a Loop
In this example, we use unshift() in a loop to add elements to an array in reverse order.
let numbers: number[] = [];
for (let i = 5; i >= 1; i--) {
numbers.unshift(i);
}
console.log(numbers); // Output: [1, 2, 3, 4, 5]
4. Conclusion
In this chapter, we explored the unshift() method for arrays in TypeScript, which is used to add one or more elements to the beginning of an array and return the new length of the array. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use unshift() effectively can help in various array manipulation tasks in TypeScript, especially when managing and modifying arrays by adding elements to the beginning.
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