Functions in JavaScript for Beginners Dev

Introduction
In this article, you will learn about function and how functions can help you avoid the repeation of codes. We discuss discuss
What a function
Types of function
The return keyword in functions
What is a function
Functions are reusable code blocks that can be called from anywhere in your application and utilized at any time, as long as they're declared.
The DRY concept (don't repeat yourself) is an important programming principle to remember. The use of functions helps to reduce the number of lines of code. In a function, we have a bracket/parenthesis that is empty without a value passed into it and one that has a value passed to it. It's called a parameter when a value is passed into the bracket/parenthesis, and it's called an argument when the function is called below and a value is passed into it.
Types of functions
I. Regular function
Ii. Arrow function.
Regular function
We have two major ways of declaring a regular function in javaScript
- Function declaration or called function statement
- Function expression
Syntax for a regular function
function name(){
}
or
let add = function(){
}
By function declaration/statement
The keyword and name of the function must be provided first. When a function is written, it is referred to as a "statement."
function add(){
console.log(2 + 3)
}
add() //ouput 5
in any function, you must call it or else it won't work.
Another example of the function declaration is passing in an argument
function name(firstName, lastName) {
console.log(firstName, lastName)
}
name("ij","cent") // output ij cent
name("sam ","white") //output sam white
- By function expression
The name of the function is utilized as variable storage in function expressions. It's an expression when it's initially first stored into a variable. Example
let add = function () {
console.log(2 + 3);
};
add(); //output 5
II. Adding a parameter in a function expression
let name = function(firstName, lastName) {
console.log(firstName, lastName)
}
name("ij","cent") //output ij cent
name("sam","white") // output sam white
- The arrow function
It is a modern technique of writing a function that most developers utilize, and the good/cool😎 thing about it is that it has a simpler syntax and doesn't require the usage of the function keyword. It is not necessary to write the function keyword.
The syntax of arrow function:-
const num=()=>{}
Example of arrow function
let name = () => {
console.log('kc');
}
name() //output kc
In general, any of the methods can be utilized; depending on your comprehension/understanding, the same effect will be obtained.
The return keyword for functions
The return keyword computes the codes, it processes the code we pass into it.
When the return keyword is used, it ends the code and no further lines of code are executed below it.
Example
function num( a,b) {
return (a+b) / 2
}
console.log(num(3,3)) // output 3




