Functions In Javascript

Functions In Javascript

a introduction of function in javascript

hey folks,

after giving you content on machine learning I just shifted my tech stack from machine learning to javascript I hope you guys give me the same support and love for these articles also.

functions are the building block for any programming language . It is a very crucial and fundamental concept in javascript. So let's discuss this fundamental as well as important concept of javascript.

Functions are the heart of javascript

Need of functions in javascript

As I mention earlier functions are the heart of javascript many fundamental as well as advanced concepts like higher-order functions, binding of functions and function invocation require the pre-requisite of functions in javascript.

Types of functions in Javascript

There are mainly three types of function or function declaration in javascript.

  1. function declaration

  2. function expression

  3. arrow function

function declaration

  1. write the function keyword and then write the name you want to give to your function and the number of parameters you wanna declare.

     function dummy_function()
    
  2. .use { } after the first step

function dummy_function()
{
}

write your logic in the braces { } you wanna write

function dummy_function()
{
console.log("hello world ");
console.log("this is my first function");
}
  1. Congrats your write your first function in javascript.

  2. calling of function

function demo_function()
{
console.log("hello world");
}
demo_function();

Function expression

function expression is another way of defining and using function in javascript .

steps for function expression

  1. just use const keyword and then write your function name
const demo_function
// here demo_function is my function name.

2. then use function keyword and required parameter for the function

const demo_function=function(/*parameters in function*/)
  1. then use braces {} and write your logic in braces
const demo_function=function()
{
console.log("hello world");
}
console.log(demo_function());

congrats you write your first function in function expression style.

ARROW FUNCTION

Arrow function is a broad concept in javascript but the scope of this article is to give you a brief introduction to the arrow function and how it is defined in javascript.

Many new beginners find the arrow function a little tuff and hard concept and practice a lot for the arrow function but I hope after reading this portion of the arrow function you can easily declare the arrow function and see how easy it is to write an arrow function.

steps for arrow function

  1. first write the const keyword and write your function name

     const demo_function
    
  2. then like you do in function expression write function and then use =>symbol

     const demo_function=function()=>
    
  3. then use braces {} and write your logic in them

     const demo_function=function()=>{
     console.log("hello and congrats for writting your first arrow function in javascript");
     }
    
  4. just remove the function keyword from the function expression like above

     const demo_function=()=>{
     console.log("hello and congrats for writting your first arrow function in js");
     }
    
  5. calling of arrow function is the same as a function expression

     const demo_function=()=>{
     console.log("hello and congrats for writting your first arrow function in js");
     }
     console.log(demo_function());
    

    arrow function is quite useful and handful and it is quite important if your are doing react js

shortcut way to write the Arrow function

  1. write function expression for your function.

  2. remove the function keyword and add => in the function expression.

  3. your arrow function is ready to use.