{"id":136282,"date":"2024-07-15T05:49:50","date_gmt":"2024-07-15T05:49:50","guid":{"rendered":"https:\/\/www.sushilkumar.ind.in\/blog\/?p=136282"},"modified":"2024-07-15T05:49:50","modified_gmt":"2024-07-15T05:49:50","slug":"what-is-higher-order-function-in-javascript","status":"publish","type":"post","link":"https:\/\/www.sushilkumar.ind.in\/blog\/javascript\/what-is-higher-order-function-in-javascript\/","title":{"rendered":"What is higher order function in JavaScript?"},"content":{"rendered":"\n<p>A higher-order function is a function that either takes another function as an argument or returns a function as a result, or both. In JavaScript, functions are first-class citizens, meaning they can be treated like any other value, such as numbers or strings. This enables higher-order functions to be a fundamental part of the language&#8217;s flexibility and power.<\/p>\n\n\n\n<p>Here&#8217;s a breakdown of the two main aspects of higher-order functions:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Accepts a function as an argument<\/strong>: A higher-order function can take one or more functions as arguments. These functions are often referred to as callback functions because they are &#8220;called back&#8221; by the higher-order function to perform a certain action. For example:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>function higherOrderFunction(callback) {\n    \/\/ Do something...\n    callback(); \/\/ Calling the callback function\n}\n\nfunction callbackFunction() {\n    console.log(\"Callback function is called\");\n}\n\nhigherOrderFunction(callbackFunction);\n<\/code><\/pre>\n\n\n\n<p><strong>Returns a function as a result<\/strong>: A higher-order function can also return another function. This allows for the creation of functions on the fly, which can be customized based on the arguments passed to the higher-order function. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function createMultiplier(multiplier) {\n    return function(number) {\n        return number * multiplier;\n    };\n}\n\nconst double = createMultiplier(2);\nconsole.log(double(5)); \/\/ Output: 10\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>In this example, <strong>createMultiplier<\/strong> is a higher-order function that takes a <strong>multiplier<\/strong> argument and returns a new function. The returned function (<strong>double<\/strong>) multiplies its input by the <strong>multiplier<\/strong> value provided to <strong>createMultiplier<\/strong>.\n\nHigher-order functions are commonly used in JavaScript for various tasks, such as iterating over arrays, handling asynchronous operations, implementing functional programming concepts like <strong>map<\/strong>, <strong>filter<\/strong>, and <strong>reduce<\/strong>, and <strong>creating<\/strong> <strong>reusable<\/strong> and <strong>composable<\/strong> code. They promote a more modular and flexible codebase by enabling the separation of concerns and encouraging the use of smaller, composable functions.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A higher-order function is a function that either takes another function as an argument or returns a function as a result, or both. In JavaScript, functions are first-class citizens, meaning they can be treated like any other value, such as numbers or strings. This enables higher-order functions to be a fundamental part of the language&#8217;s &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[323],"tags":[],"class_list":["post-136282","post","type-post","status-publish","format-standard","","category-javascript"],"jetpack_publicize_connections":[],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p99pkJ-zs6","_links":{"self":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136282"}],"collection":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/comments?post=136282"}],"version-history":[{"count":1,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136282\/revisions"}],"predecessor-version":[{"id":136403,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136282\/revisions\/136403"}],"wp:attachment":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media?parent=136282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/categories?post=136282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/tags?post=136282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}