EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript reduce
 

TypeScript reduce

Updated April 6, 2023

TypeScript reduce

 

 

Introduction to TypeScript reduce

Whenever there is a need to reduce the multiple values stored in the array to a single value by performing a function on two elements of the array at once starting from the left, we make use of a function called reduce function in TypeScript. It takes two parameters, namely a function along with parameters which are going to be called back again and again and then the return value from this function which acts as a first parameter to the function in the next call back, and this reduce function returns a single value as a result after performing the function on all the elements of the array considering two elements at once.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax to declare reduce function in TypeScript:

reduce(function_that_is_called_again_and_again(parameter1, parameter2)
{
return parameter1
}

Where function_that_is_called_again_and_again is the function along with parameters paramter1 and parameter2, which is going to be called back again and again until all the elements of the array are traversed, and then the return value from this function acts as the first parameter to the function in the next call back.

Working of reduce Function in TypeScript

  • An array in TypeScript is used to store different values of similar datatypes in a linear fashion.
  • If a need arises to reduce the multiple values stored in the array to a single value, we use a function called reduce function in TypeScript.
  • The reduce function performs operation or function on two values of the array at a time, starting from left to the right.
  • The reduce function takes two parameters, namely a function and then the return value from the function, which acts as the first parameter to the function itself in its next call back.
  • The function passed as a parameter to the reduce function keeps executing again and again until all the elements of the array are traversed.
  • The reduce function returns a single value resulting from performing an operation on the elements of the array.

Examples of TypeScript reduce

Given below are the examples of TypeScript reduce:

Example #1

TypeScript program demonstrates reduced function, which multiplies two elements of the array at once to find out the product of all the elements in the array and display the resulting value as the output on the screen.

Code:

//defining an array called arrayname to store the elements in the array
var arrayname = [ 1, 2, 5, 10 ];
//defining a variable called result to store the return result from reduce function whose firs   t parameter is a function to compute the product of two numbers and their return result is passed as a first parameter to the function itself
var result = arrayname.reduce(function(firstparam, secondparam)
{
return firstparam * secondparam;
});
//displaying the result of the reduce function as the output on the screen
console.log('The product of all the elements of the array is: ');
console.log( result );

Output:

TypeScript reduce 1

In the above program, an array called arrayname is defined to store the elements in the array. A variable called result is defined to store the return result from a reduced function whose first parameter is a function to compute the product of two numbers. Their return result is passed as a first parameter to the function itself. Then the result of the reduce function is displayed as the output on the screen.

Example #2

TypeScript program demonstrates reduced function, which divides two elements of the array at once to find out the result of performing division on all the elements in the array and display the resulting value as the output on the screen.

Code:

//defining an array called arrayname to store the elements in the array
var arrayname = [ 16, 4, 2 ];
//defining a variable called result to store the return result from reduce function whose firs   t parameter is a function to compute the division of two numbers and their return result is passed as a first parameter to the function itself
var result = arrayname.reduce(function(firstparam, secondparam)
{
return firstparam / secondparam;
});
//displaying the result of the reduce function as the output on the screen
console.log('The result of performing division on all the elements of the array by taking two elements at once is:');
console.log( result );

Output:

TypeScript reduce 2

In the above program, an array called arrayname is defined to store the elements in the array. A variable called result is defined to store the return result from a reduced function whose first parameter is a function to compute the division of two numbers. Their return result is passed as a first parameter to the function itself. Then the result of the reduce function is displayed as the output on the screen.

Rules and Regulations

Given below are the rules and regulations for using reduce function in TypeScript:

  • The parameters to the reduce function must be a callback function whose return result acts as a first parameter to the function itself.
  • The reduce function can be used on an array consisting of more than one value only.
  • The reduce function can perform the function on only two elements of the array at once.
  • The reduce function can perform the function on two elements of the array at once, starting from left to right.
  • The resulting value from the reduce function can be of any data type like float, double etc.
  • The resulting value is infinity in cases when the function cannot be performed on the elements of the array.

Recommended Articles

We hope that this EDUCBA information on “TypeScript reduce” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. TypeScript let
  2. TypeScript typeof
  3. TypeScript Cast Object
  4. TypeScript Array

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW