JavaScript Function , First Class function / First Class Citizen in JavaScript

Md Pervez Hossain - May 22 - - Dev Community

What is a Function in JavaScript?
In JavaScript, a function is a block of code designed to perform a particular task. A function is executed when "something" invokes it (calls it).

Image description

What is an Anonymous Function?
An anonymous function is a function that does not have a name. Writing an anonymous function as function() {} directly will result in a syntax error because function statements require a function name. Anonymous functions are used where functions are treated as values. You can use anonymous functions in function expressions, where the function is assigned to a variable. In this case, the anonymous function acts as a value assigned to the variable.

Image description

What is the Difference Between Function Statement and Function Expression?
The major difference is hoisting. Function statements are hoisted, meaning they can be called before they are defined in the code. Function expressions, including anonymous functions, are Also hoisted, but in the Function Expressions , function assign a variable as like a value . in the memory Creation Phase initially it sets "Undefined". when you try to invoke the function before initialed , it throw reference Error that is "sayHello is not a function"

Image description

What is a Function Declaration?
A function declaration is a standard way to declare a function using the function keyword, followed by the function name and a parameter list. Function declarations are hoisted in their scope.

Image description

What is a First-Class Function in JavaScript?
First-class functions are functions that can be treated as values. They can be passed as arguments to other functions, returned from Others functions, and assigned to variables As Value. the Kind of those abilities of Function is known as First class Function in JavaScript. first Class Function is Also Known as First Class citizens.
Key Points of First Class Function :
1. Passed As Arguments : Here hello function passed as an argument of test function.
Image description

2. Returned from others functions : Here x function is Returned from the hello function and hello function is assign to result variable means x function assign to result variable as value.
Image description

3. Assign to variable as like a value :
Image description

4. Also known is First Class citizen

What is a Named Function Expression?
A named function expression is an anonymous function assigned to a variable but with a name. This name can be used for internal references within the function body.

Image description
but here you can not call the x function global cope because During the memory creation phase , here x , is named function expression which is allocated in the memory as the value of the let variable, so when you call x() function, it throws Reference error, that is x is not defined. because x is not defined as a global function . It created a local variable . You can call this x() function inside the x function, you got the whole function as output.

Function Parameters and Function Arguments
Parameters: Identifiers or labels that receive values inside the function. They are local variables of the function and cannot be accessed outside of it.
Arguments: The actual values passed into the function.

Image description

. . . . . . . . . . . . . . .