Sign in
Log inSign up
What is JavaScript function?

What is JavaScript function?

Adedotun Adedigba's photo
Adedotun Adedigba
·Apr 30, 2021·

3 min read

You are here probably because you want to learn about JavaScript functions or want to know more about it, either ways I got you. JavaScript function is a statement that performs a task and it must have an input and return an output while called to action.

Declaring a function

To declare a fucntion you need the :

• The "function" keyword.

• The name you wish the function is called.

• The parameters to be used in the function.

• The statements to be executed by the function, they are enclosed in curly brackets {...}.

check the code below:

function  add(a,b){
 return a + b
 }

NOTE: In the code above it has the function keyword, the function name add, the parameters to be used which are (a, b), the statement to be executed in the curly brackets which is a + b and the the return keyword is also important as it executes and specifies a value to be returned.

Expressing Functions

The previous paragraph explained how function is declared, just so you know function can be created by expression also i.e assigned to a variable.

const Add = function(a, b) {
  return a + b
}

NOTE: There's a little change? yes, our function doesn't have a function name, these functions are "anonymous" functions and they can be called for action ny just calling the variable name i.e Add()

Calling a Function

What is calling a function? it is passing arguments to the function and make it perform the specified actions. For example our "Add" function is called by writing this syntax:

Add(1,2)

Function Scope

So function scope talks about variable accessibility, variables defined inside of a function cannot be accessed from anywhere outside the function. Functions have access to all variables defined inside the same scope which it's defined.

var a = 1, b = 2;

function Add(a, b){
 return a + b
}
Add()

Function Recursion

Function Recursion is the process whereby a function can refer to and call itself.
A function that calls itself is called a recursive function is called a recursive function. Recursion is similar to a loop as they both execute same code multiple times and to do this they need a condition they need a condition to do this. see the example below:

function repeat(a){
  if(a <= 5) // The condition for execution. 
  return a + 1;
  repeat ( a + 3); // the recursive call
}

repeat (1)

Function Closures

A function closure is a function nested in another function and can access the variable defined in its parent function.

/* this will throw an error because 
the outer world doesn't
 know c exists */
function  add(a, b) {
  let c = a + b;
  return c;
}

console.log(c)

We know the above code won't work, why? just because the variable "c" only lives in the function "add" world(world here depicts scope). so being able to access a specific instance of local binding in enclosing scope is called closures.

function add(a, b){
  let c = a + b;
// addOne is a closure 

function addOne(){
  return c + 1;
}
return addOne()
}

console.log(add(1,2))

Conclusion

so that's it for functions in this article, to learn more about functions you can check this out: developer.mozilla.org/en-US/docs/Web/JavaS…

Thanks, keep practicing see you at the top