In, one of our previous sessions, We had seen, What is a Function, and in this session, We are going to see, how can we declare a function in different ways.
This is a type of function which we have already discussed in our previous session. Let’s see the example and syntax for the same.
function add() {
console.log(2+3);
}
add();
This type of function will have 4 types which we already discussued in our previous session.
Let’s see the example for one of them.
function add(x,y) {
return x+y;
}
const sum = add(2,3);
console.log(sum);
Function Expression is a kind of Anonymous function.
Let’s see How to create this.
function() {
console.log(2+3);
}
Now, you see that there is no name in this function, then how can we call this type of function, So we do it this way.
const add = function() {
console.log(2+3);
}
add();
Now, this function has got a name add, and we can call it with that name.
This type of function will also have 4 types which we already discussued in our previous session
Let’s see the example for one of them.
const add = function(x,y) {
return x+y;
}
const sum = add(2,3);
console.log(sum);
This is the most popular type of function.
This function is somewhat same like function expression.
We don’t need to write function keyword when we declare an arrow function.
We use arrow keys in place of function keyword.
const add = () => {
console.log(2+3);
}
add();
const add = (x,y) => {
return x+y;
}
const sum = add(5,6);
console.log(sum);
How do we create a Constructor in other languages like Java, We will use new keyword to create a Constructor. Same way, If we have to create a Function Constructor, We will need to use a new keyword.
Rules for creating a Function Constructor.
So, we will write a Function Constructor, something like this.
new Function('x', 'y', 'return x+y')
But again, we see, that there is no name for the function, so how to call this. So, we do it something like this.
const add = new Function('x', 'y', 'return x+y')
const sum = add(8,9);
console.log(sum);
Note – This type of function is not used much, but we must know for knowledge purpose.
A self invoking function is called IIFE (Immediately Invoked Function Expression). this type of function is written and invoked immediately, so, this is called the self invoking. This type of function will be executed itself. We don’t need to call it.
Write a paranthesis, write function keyword inside paranthesis and declare a function like a normal function. Come outside the paranthesis and write a paranthesis and put a semicolun.
(function() {
console.log("This is Self Invoking function")
})
();
So, You can see, We are not calling it, We are not giving any name to this function, Simple clear function. Let’s run this.
A function that returns an iterator object for generating a sequence of values is called Generator Function.
Rules for declaring a Generator Function – Use function keyword with wildcard and use a keyword called yield.
function* generateNumberSequence() {
yield 1:
yield 2:
yield 3:
yield 4:
}
const generator = generateNumberSequence();
console.log(generator.next().value);
console.log(generator.next().value);
console.log(generator.next().value);
console.log(generator.next().value);
Note – This is also not much important but just know this for knowledge purpose.
Anonymous function is a type of function which has no name (Identifier).
Callback function and function expression are the example of such functions.
function expression, We have already seen.
Callback function, We will see in our coming sessions, when we will discuss about Asynchronization in Javascript.
For now, If you want to understand, What is an Anonymous function, Then we can simply say, that a function without any name is an anonymous function, Now, How can we run an anonymous function, because it doen’t have any name, So we can write an anonymous function inside a function, and how do we do that, Let’s see it.
So, Let’s say, We have an array and we want to print each and every element of the array using anonymous function, then we can write it something like this.
let num = [3, 5, 1, 2, 4]
num.forEach(function(ele) {
console.log(ele);
})
Recursive Function is a type of function which calls itself.
Let’s see the example of a Recursive Function.
function doSomething(n) {
if(n === 0) {
console.log("TASK COMPLETED!")
return
}
console.log("I'm doing something.")
doSomething(n - 1)
}
doSomething(3);
A function that takes 1 or more functions as an argument.
So, Let’s write a normal function first.
function addition(a, b) {
console.log(a+b);
}
Now, Let’s write a Higher Order Function.
function operate(funcName, a, b) {
funcName(a,b);
}
Now, Let’s call the Higher Order Function.
operate(addition, 5, 7);
We have covered below topics in this session
Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…
We will start from object oriented programming basics like What are different concepts in Object…
Why we can not over ride a Constructor This is a very important question, that,…
What is Arrow Function In the last session, We had discussed about Anonymous Function. Anonymous…
What is Anonymous Function Anonymous Function is a type of function which has no name…
What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…