DEV Community

Nanthini Ammu
Nanthini Ammu

Posted on

JS Functions

Function :

  • Reusable blocks of code designed to perform specific tasks
  • Function run when it is called.

Example :


function greet() {
  console.log("Hello!");
}

greet(); 

Output : Hello!

Enter fullscreen mode Exit fullscreen mode
  • Variables(let) inside function are private.
function test() {
  let x = 10;
}

console.log(x); //error
Enter fullscreen mode Exit fullscreen mode

Functions with Parameters :

  • Parameters allow to pass values to a function.
  • Parameters are listed inside the parentheses in the function definition.

Parameter : Variable in function definition
Argument : Actual value passed to function


function add(a,b) {   //  'a,b' is Parameter
  console.log(a+b);
}

add(5,6);      // "5,6" is Argument

Enter fullscreen mode Exit fullscreen mode

function greet(name) {
  console.log("Hello " + name);
}

greet("Varun");

Output : Hello Varun

Enter fullscreen mode Exit fullscreen mode

Defautlt Parameter :

  • Default parameters allow to assign a default value to a function parameter.
  • If no argument is passed, default value is used.
  • Always keep default parameters at the end.
function display(name = "You"){

    console.log(name);
}

display("Varun");
display();

Output : 
Varun
You
Enter fullscreen mode Exit fullscreen mode

Multiple Defautlt Parameters :

function add(a = 0, b = 0) {
  return a + b;
}

console.log(add(5, 3)); // 8
console.log(add(5));    // 5
console.log(add());     // 0
Enter fullscreen mode Exit fullscreen mode

Return Values :

  • A function can return a value back to the code that called it.
  • When a function reaches a return statement, the function stops executing.The value after the return keyword is sent back to the caller.

function add(a, b) {
  return a + b;
}

let result = add(5, 3);
console.log(result); 

Outpu : 8
Enter fullscreen mode Exit fullscreen mode

Function Types

  1. Function Declaration.
  2. Function Expression.
  3. Arrow function.

Function Declaration :

  • The traditional way to define a named function. It is hoisted, meaning it can be called before its definition in the code.

let result = add(5, 3);
console.log(result); 

function add(a, b) {
  return a + b;
}

Enter fullscreen mode Exit fullscreen mode

Function Expression :

  • It is a function stored in a variable.
  • After a function expression has been stored in a variable, the variable can be used as a function.
  • It is a function without a name. It ends with a semicolon.
  • This is not hoisted.As function expressions can not be called before they are defined.

const multiply = function(a, b) {
  return a * b;
};
console.log(multiply(4, 3));

Enter fullscreen mode Exit fullscreen mode

Arrow Function :

  • It allows a simple syntax for function expressions.
  • Can create it without function, return keywords and curly brackets.
  • It can be used when the function body contains only one statement.
  • Parentheses are required for zero or multiple parameters.
  • It is not hoisted.

const multiply = (a,b)=> a*b;
console.log(multiply(1,2));

//Can also do this


const operator = (a,b) => {
    console.log(a+b); 
    console.log(a*b);
    console.log(a/b);
    console.log(a%b)
};
operator(1,2);


Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Your definition of Function Expression is not correct. Function expression and storing a function in a variable are 2 different things. You have not explained what function expression actually is.