Function Expression
A function can also be written in the form of an expression. Such a function may not have a name and is called anonymous. This expression has helpful when we need to pass a function as a parameter.
For example:
const add= function(number1, number2) { return number1 * number2 }
var x = add(4, 5) // x gets the value 9
The function expression "add" can be assigned to a variable and return the two numbers passed.
function test(fac, a) {
let result = []; // Create a new Array
let i; // Declare variable
for (i = 0; i != a.length; i++)
result[i] = fac(a[i]);
return result;
}
The test function uses the passed function "fac" in the body.
Calling functions
Like every other function, in Node.js also a function has to be called to activate.
When we call a function, the function performs the specified actions with the indicated parameters.
For example:
add(5,4);
In the above example, the function call statement takes two arguments and returns 9, the sum of numbers 5 and 4.
A function can be called as long as it is in the scope. The function declaration can also be made after calling the function.
console.log(square(5));
/* ... */
function square(n) { return n * n );
Scope of the function
Variables defined inside a function can't be accessed from outside the function since the variable is only defined within the function's scope. On the other hand, a function has access to all variables and functions specified within the scope in which it was defined.
In other words, a function created in the global scope has access to all global scope variables. All variables declared in its parent function and any other variables to which the parent function has access are accessible to a function defined inside another function.
//the global scope variables
var a = 20,
b = 3,
str = 'JavaScript';
// the global scope function
function multiply() {
return a* b;
}
multiply(); // Returns 60
// A nested function
function getScore() {
var a = 2,
b= 3;
function add() {
return str+ ' scored ' + (a + b);
}
return add();
}
getScore(); // Returns "Chamakh scored 5"
Nested Functions
We can nest a function inside another function. The enclosing (outer) function has access to the nested (inner) function. The arguments and variables of the contained function can be "inherited" by a nested function. To put it another way, the inner function contains the outer function's scope.
function addSquares(a, b) {
function square(x) {
return x * x;
}
return square(a) + square(b);
}
a = addSquares(2, 3); // returns 13
b = addSquares(3, 4); // returns 25
c = addSquares(4, 5); // returns 41
Naming conflicts
A name conflict occurs when two arguments or variables in the scopes of a closure have the same name. The scopes with the most nested scopes take precedence.
As a result, the innermost scope takes precedence, while the outermost scope takes precedence. The scope chain is as follows. The innermost scope is the first on the chain, while the outermost scope is the last.
Example:
function outside() {
var x = 5;
function inside(x) {
return x * 2;
}
return inside;
}
outside()(10); // returns 20 instead of 10
Frequently asked questions
1. How do you code a function in JavaScript?
Ans: To declare a function, you use the function keyword, followed by the function name, a list of parameters, and the function body
2. What are the types of functions in JavaScript?
Ans: JavaScript has 3 types of equals that are different from each other.
3. What are functions in JavaScript?
Ans: In JavaScript, a function is comparable to a procedure, a series of instructions that performs a task or calculates a value, but a process must accept some input and return an output with some evident relationship between the input and the output to qualify as a function.
Key takeaways
A javascript function starts with a “function” keyword followed by the name of the function.
A function can also be written in the form of an expression.
This article was all about functions in Node js. If you want to learn advanced front end web development, Coding Ninjas has one of the best courses available, which you can find here.
Happy Learning!