Functions in JavaScript
Normal functions , Anonymous functions ,Arrow functions
Topics to be discussed: Functions, passing functions, anonymous functions, arrow functions.
What do you mean by functions?
function print(name) {
return "Hello " + name
console.log("this will never execute")
}
We can call (invoke) this function like this Once we have returned something it will go back to the previous call and return the value to it. Here we have a single call of Dad function only. But if you are aware of recursion, you already know, why I am talking about the previous call.
print()
Passing function to another function
function Dad(text2, son) {
son("hello " + text2)
}
function son(text1) {
console.log(text1)
}
Dad("saurav", son)
We created a function Dad and while calling it we passed a string and another function "son".
But if we pass son()
instead of son,
it will return undefined
because it does not return any value. We can use +
symbol to concatenate strings. Inside our function, we are calling our function son
.
console.log("any text")
is used to print the value in Javascript
Also, we can treat functions as variables
let a = function sum(){
return a+b
}
console.log(a)
It will print the exactly same thing that is written inside the ``` variable a
# Anonymous function
It is that function which doesn't have any name. It will be used more in case of arrow function. Instead of passing name of variable into calling function , we can create an anonymous function.
<br>
### If we don't use anonymous function
```JAVASCRIPT
function a(name){
b("hi "+name)
}
/*
we can use any variable .. bcoz it is a prototype
but to understand it without any confusion I am passing name
*/
function b(name){
console.log(name)
}
a("saurav")
If we use an anonymous function
function a(text,anyNameHere){
anyNameHere("hi "+text)
}
/*
we can use any variable .. bcoz it is a prototype
but to understand it without any confusion I am passing name
*/
// using two arguments bcoz its prototype also has two arguments
//"saurav" willl be copied inside name variable
//anonymous function will be copied inside anyNameHere variable
a("saurav",function b(name){
console.log(name)
})
Arrow Functions
Arrow functions are similar to anonymous functions. It makes our code less bulky and improves our readability.
//called or callee function
function abc(x , anyNameHere){
anyNameHere(x)
}
// calling function
abc("saurav", (name)=>{
console.log(name)
})
It can be used anywhere. If you don't have any arguments then you can use like this
let variableName = ()=> console.log("I have no arguments " )
// parenthesis are optional if you don't have any argument or single arguments
//calling function
variableName( )
If you have more than one argument then you can use like this
// function sum(a, b) {
// return a + b
// }
//same thing can be done with the help of arrow functions
let sum = (a, b) => a + b
//don't add return in case of single line statement in arrow function
//bcoz arrow function automatically adds return in its first line
console.log(sum(2, 3))
Output
5
let func = (a, b) => {
a = a + b
return "new value of a is " + a
}
console.log(func(2, 3))
new value of a is 5
Both the normal function and the arrow function show different behaviour while using the "this" keyword. I will add this topic later.
Comment down below if I have made any mistake.