My Docs
Array-Methods-Lesson
Array-Methods-Lesson
  • Home
    • General Questions
    • Lesson Plan
    • Advice
  • ๐Ÿ‘จ๐Ÿ’ป Methods
    • ๐Ÿ”—Array.forEach()
      • forEach-Questions
    • ๐ŸšฎArray.filter()
      • Filter Questions
    • ๐Ÿ’ฉArray.reduce()
      • Reduce Questions
    • ๐Ÿ—บ๏ธArray.map()
      • Map Questions
    • Native Implementation
  • Background
    • ๐Ÿ˜ถMutability
    • ๐ŸผArray Basics
    • ๐Ÿ”™Background
    • ๐Ÿ“ฒCallback Functions
    • ๐Ÿ”๏ธHigher Order Functions
    • ๐Ÿง˜Difference Between Functions & Methods...
    • โžก๏ธFat Arrow Syntax
    • ๐Ÿ’ฑDoes It Mutateยฟ
    • First Class Functions
  • ๐Ÿ“–Resources
    • ๐Ÿ“ฐCheat Sheet
      • ๐Ÿ‘ทโ€โ™€๏ธ๐Ÿ‘ทโ™€ ๐Ÿ‘ทโ™€ ๐Ÿ‘ทโ™€ ๐Ÿ‘ทโ™€ Examples
    • ๐Ÿค“Other Array Methods
    • Examples
      • Refactor
  • ๐Ÿ‘ฝMiscellaneous
    • ๐ŸArray Methods Explained As Emojis
Powered by GitBook
On this page
  • First-class Function
  • Example | Assign a function to a variable
  • Example | Pass a function as an Argument
  • Example | Return a function

Was this helpful?

Edit on GitHub
  1. Background

First Class Functions

PreviousDoes It MutateยฟNextCheat Sheet

Last updated 3 years ago

Was this helpful?

First-class Function

A programming language is said to have First-class functions when functions in that language are treated like any other variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.

const foo = function() {
   console.log("foobar");
}
foo(); // Invoke it using the variable
// foobar

Copy to Clipboard

We assigned an Anonymous Function in a , then we used that variable to invoke the function by adding parentheses () at the end.

Note: Even if your function was named, you can use the variable name to invoke it. Naming it will be helpful when debugging your code. But it won't affect the way we invoke it.

function sayHello() {
   return "Hello, ";
}
function greeting(helloMessage, name) {
  console.log(helloMessage() + name);
}
// Pass `sayHello` as an argument to `greeting` function
greeting(sayHello, "JavaScript!");
// Hello, JavaScript!

Copy to Clipboard

We are passing our sayHello() function as an argument to the greeting() function, this explains how we are treating the function as a value.

function sayHello() {
   return function() {
      console.log("Hello!");
   }
}

Copy to Clipboard

In this example; We need to return a function from another function - We can return a function because we treated function in JavaScript as a value.

Note: A function that returns a function is called a Higher-Order Function.

Back to our example; Now, we need to invoke sayHello function and its returned Anonymous Function. To do so, we have two ways:

Note: The function that we pass as an argument to another function, is called a . sayHello is a Callback function.

Example | Assign a function to a variable
JavaScript
Variable
Example | Pass a function as an Argument
JavaScript
Callback function
Example | Return a function
JavaScript