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

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

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

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?