Skip to main content

Command Palette

Search for a command to run...

Functions in JavaScript for Beginners Dev

Updated
3 min read
Functions in JavaScript for Beginners Dev
I
Ijeoma breaks down programming, cloud, and DevOps concepts into simple, practical, and relatable explanations, making tech easier to understand for beginners, self-taught developers, and the curious.

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.

  1. 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
  1. 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.

  1. 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

More from this blog

Frontend dev | Tech Writer

35 posts

Ijay front-end developer... Tries to explain things in simple way.

https://linktr.ee/i_jeoma