TypeScript Array every()

In this chapter, we will explore the every() method for arrays in TypeScript. This method is a built-in function that tests whether all elements in the array pass the test implemented by the provided function. Understanding how to use every() is useful for performing validations and checks on arrays.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a boolean value indicating whether all elements in the array satisfy the condition.

2. Syntax

array.every(callback(element, index, array), thisArg?);

Parameters

  • callback: A function to test for each element, taking three arguments:
    • element: The current element being processed in the array.
    • index (optional): The index of the current element being processed in the array.
    • array (optional): The array every was called upon.
  • thisArg (optional): Value to use as this when executing callback.

Return Value

The method returns true if the callback function returns a truthy value for every array element; otherwise, it returns false.

3. Examples

Let's look at some examples to understand how every() works in TypeScript.

Example 1: Basic Usage

In this example, we check if all elements in the array are greater than 10.

let numbers: number[] = [12, 14, 16, 18];
let result = numbers.every((num) => num > 10);
console.log(result); // Output: true

Example 2: Check for Even Numbers

In this example, we check if all elements in the array are even.

let numbers: number[] = [2, 4, 6, 8];
let result = numbers.every((num) => num % 2 === 0);
console.log(result); // Output: true

Example 3: Using Index and Array in Callback

In this example, we use the index and the array itself within the callback function.

let numbers: number[] = [1, 3, 5, 7];
let result = numbers.every((num, index, arr) => {
  console.log(`Element: ${num}, Index: ${index}, Array: ${arr}`);
  return num % 2 !== 0;
});
console.log(result); // Output: true
// Console will also log each element, index, and array.

Example 4: Empty Array

In this example, we check the behavior of every() on an empty array.

let numbers: number[] = [];
let result = numbers.every((num) => num > 0);
console.log(result); // Output: true

Example 5: Array of Objects

In this example, we check if all objects in an array have a specific property value.

interface Person {
  name: string;
  age: number;
}

let people: Person[] = [
  { name: "Ravi", age: 25 },
  { name: "Ankit", age: 30 },
  { name: "Priya", age: 28 },
];

let result = people.every((person) => person.age > 20);
console.log(result); // Output: true

4. Conclusion

In this chapter, we explored the every() method for arrays in TypeScript, which is used to test whether all elements in the array pass the test implemented by the provided function. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use every() effectively can help in various array validation tasks in TypeScript, especially when checking if all elements in an array meet certain conditions.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare