Javascript Functions: Introduction
Before talking about Function in Javascript, You must know about the basic terms of Javascript. i.e Input, output, execution, variables, data types, prompt, alert and so on. Suppose, if you have to perform any operation multiple times in a program, you assigned the variable whenever required for the same operation. To avoid this repetition of declaring variables for the same operation, you can make a function and call it, whenever it required. let’s know more about function.
What is Function?
A set of statements that perform a task is said to be a Function, the task can be any calculation or any mathematical operation. You can call the function in a program while needed. The structure of Function looks like,
it has a function keyword, followed by the function name and after that, there is a parameter inside the parenthesis and then curly braces contain the statements. You can call a function by putting parentheses after the function name. So, every expression having parentheses is a function. console.log() is also a function which is predefined in Javascript to display the output on console.
Naming Function
Now, talk about the name of the function, Naming functions is extremely useful for debugging purposes. Name of the function should be related to its task of what the function is for. so that the other coder can easily understand the use of the function and he can easily access the code. this is how it should be.
Function as values
The most important features of functions are that they can be defined and invoked. In Javascript, a program is said to be a First class function if their functions are treated like as any other variables, it means, functions can be passed as an argument in other function and can be returned by another function. It can also be stored in the properties of objects or the element of an array. we can easily understand with the given function below.
this function creates a new function object which is assigned in a variable named square.it is just a name which refers to the function object. if we change the name “s” instead of square, it still works the same way. let see:
Arrow Function
There are two ways of writing function, i.e Declare function and Expression function(arrow function). Arrow function is similar to the declare function, the only thing which makes the difference between them is their syntax. you can say, it is the shorter form of declare function. It was added in 2015 to make it simple. In arrow function, the function name is declared, assigned with the parameter in parentheses and then there is an arrow( => ) followed by the function body. it looks like:
Originally published at https://ashwanig3.github.io/blog/javscript_functions on September 6, 2018.