
#1 Functions in JavaScript
Normal functions , Anonymous functions ,Arrow functions
23 October, 2022
2
2
0
Contributors
Topics to be discussed : Functions , passing functions , anonymous function ,arrow function.
What do you mean by functions?
Functions removes redundancy/repetition of code. Javascript functions are created using function keyword, followed by name of the function. Here we don't need to specify the return-type of functions like we do we in C++ ,JAVA .
And we can call (invoke) this function like this Once we have returned something it will go back to 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 previous call.
Passing function to another function
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
beacause it is not returning 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 value in Javascript
Also we can treat functions as variablesIt will print the exactly same thing that is written inside the variable a. Try it Yourself.
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.
If we don't use anonymous function
If we use anonymous function
Arrow Functions
Arrow function are similar like anonymous functions. It makes our code less bulky and improve our readability .
It can be used anywhere. If you don't have any arguments then you can use like this
If you more than one arguments then you can use like this
Output
If you want to use multilines in arrow functions , then try this. You can can return in multilines.
Both normal function and arrow function shows different bahaviour while using "this" keyword. I will add this topic later .
Comment down below if I have done any mistake.
Also comment down if I have committed any mistake.
Let's connect on my socials . Always Open for new opportunities ,if I am free :P
developers
javascript
github
beginner
buildinpublic