{"id":11071,"date":"2020-09-08T10:49:29","date_gmt":"2020-09-08T14:49:29","guid":{"rendered":"https:\/\/www.carnaghan.com\/?p=11071"},"modified":"2023-10-14T20:01:49","modified_gmt":"2023-10-15T00:01:49","slug":"understanding-higher-order-functions-in-javascript","status":"publish","type":"post","link":"https:\/\/www.carnaghan.com\/understanding-higher-order-functions-in-javascript\/","title":{"rendered":"Understanding Higher Order Functions in JavaScript"},"content":{"rendered":"\n
A function is an organized block of code that is created for a specific task. It can accept and return values. The main purpose of using functions is for code reusability and organization. In JavaScript, functions can be considered ‘higher-order’. To better understand this term, we need to look at some basic concepts related to JavaScript functions.<\/p>\n\n\n\n
Take a look at the following code snippet:<\/p>\n\n\n\n
function demo(){\n console.log(\"This is a demo function\")\n}\ndemo()\n<\/code><\/pre>\n\n\n\nThis is a very basic example of a JavaScript function. The name of the function is “demo” and its prints “This is a demo function!” in the console. We we can do a lot more with this function, including assigning it to a variable, or passing it to another function as a parameter. This is possible because JavaScript is a functional programming language.<\/p>\n\n\n\n
Being a functional language, JavaScript treats functions as objects:<\/p>\n\n\n\n
function demo(){\n console.log(\"This is a demo function\")\n}\n\ndemo.property = \"This is a property\"\n\nconsole.log(demo.property)\n<\/code><\/pre>\n\n\n\nJust like we add a property to a JavaScript object, a property is added to the “demo” function.<\/p>\n\n\n\n