Here in this post we are going to see what is a function and how to write function in Javascript.
In Javascript function plays a major role. So it is important to understand how to write functions.Function is a collection of statements that do a specific task.
Writing a function.
we start the function with the keyword function. and continued with the function name. And then we have to pass parameters in parenthesis.
Parameter means the variables that are going to be used in the function. After that we have to start a curly braces to start our statements to do. Then a returning value. This value will be returned by the function.
Here is a sample function
function functionName (variablesToUse. InTheFunction){
//the codes and statments to exectue
}
Now lets make a function to add two numbers
function addNum (num1, num2){
return num1 + num2
}
There is another way of writing function that method is known as arrow function method
Arrow functions were introduced in ES6.
Arrow functions allow us to write shorter function syntax:
here it is normal function
hello = function() {
return "Hello World!";
}
here it is with arrow function
hello = () => {
return "Hello World!";
}
For more about arrow function feel free to check here.
No comments:
Post a Comment