Getting Started with Javascript Functions for Beginners Dev

Getting Started with Javascript Functions for Beginners Dev

ยท

3 min read

Hello ๐Ÿ‘‹๐Ÿฝ developers, guess you doing great?

In this article, I will be discussing:

i. What is a function

ii. What are the Various Ways of Declaring a Function

iii. What is a return keyword in a function

What is a function?

A function is a reusable code block that can be called from anywhere in your application and utilized at any point in your program as long as it is declared.

A function can have or not have an input, but it must always return an output, i.e., when declaring a function, the function keyword is written first, followed by the function name declared, then the bracket or parenthesis, a value is passed into that bracket, and that value is known as the parameter. The value passed outside the function code to call or invoke the function is referred to as an argument

An example of a function that is declared without a value or parameter is:

function myFunc() {
    console.log('john');
}
myFunc();     // output john

An example of a function that has a value/parameter passed is:

function myFunc(name) {
    console.log(name)
}
myFunc('Andrew');  //output is Andrew

What are the various Ways of Declaring a Function?

There are several ways to declare a function, which are as follows:

  • By the Name function also called function declaration or function statement.

  • By anonymous function also known as a function expression

Named Function or Function Declaration

Named function, also known as a function declaration, begins with the function, which is the keyword, followed by the name, and then a bracket or parenthesis with or without a parameter passed.

Example

function add(sum1, sum2) {
        console.log(sum1+ sum2);
}
 add( 2, 3);   //output 5

Anonymous Function

An anonymous function is a function that is stored inside a variable, it has no name as I said previously it is stored inside a variable.

Example1: shows without any value passed into the parenthesis

let add = function () {
    console.log(2 + 4);
};
add();     //output 6

Example2: shows a value passed into the parenthesis

let name = function(firstName, lastName) {
console.log(firstName, lastName)
}
name("ij","cent")  // output ij cent

Arrow function

The arrow function is the modern way of declaring a function; it is a shorter version of declaring an anonymous function that is widely used by programmers. The function keyword is omitted in the arrow function.

Example1: shows how it is written without any value passed into the parenthesis

let name = () => {
    console.log('kc');
}
name()    //output kc

Example2: shows how it is written if a value is passed into the parenthesis

let name = (firstName) => {
    console.log(firstName);
};
name('Jay');    //output Jay

What is a return keyword in a function?

The return keyword computes the written code, processes it, and ends the line of code. When the return keyword is used, no other lines of code passed under it will run in that function.

Example

function num(a , b) {

    return (a+b) / 2
}
console.log(num(3, 3))  // output 3

Conclusion

  • Always remember that for a function to return an output, it must be called or invoked.
  • Understanding the differences between Named function and Anonymous functions help you choose the right syntax for specific needs.

Do you prefer a named function or an anonymous function? Tell me why in the comments section! or Send me a mail: Gmail

Follow on: LinkedIn Twitter

Did you find this article valuable?

Support Cent Blog </๐Ÿ’–> by becoming a sponsor. Any amount is appreciated!

ย