Passing function as arguments

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 141:- Passing function as arguments

Passing functions as arguments is a common technique in JavaScript, and it allows for greater flexibility and modularity in your code.

In JavaScript, functions are first-class citizens, which means that they can be treated like any other value or data type, including being assigned to a variable, passed as an argument to another function, or returned as a value from a function.

Here's an example of passing a function as an argument:

cssCopy code

function add(a, b) {  return a + b; } function multiply(a, b) {  return a * b; } function doMath(operation, a, b) {  return operation(a, b); } console.log(doMath(add, 2, 3)); // Output: 5 console.log(doMath(multiply, 2, 3)); // Output: 6

In the example above, we defined three functions: add, multiply, and doMath. The doMath function takes an operation function and two arguments, and it applies the operation function to the two arguments.

We can pass different functions to the doMath function to perform different operations on the two arguments. In this case, we passed the add and multiply functions as the operation argument to doMath, and the function applied the appropriate operation to the two arguments.

16. JavaScript - Functions and Array

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support