DEV Community

Cover image for Functions in JavaScript
Harini
Harini

Posted on

Functions in JavaScript

What is Functions?

  • Fundamental building blocks of all programming.

  • Reusable block of code designed to perform a particular task.

  • Functions are executed when they are called.

Example

function greet(){
   return "Hello";
}
console.log(greet());
Enter fullscreen mode Exit fullscreen mode

Function Expression

  • A function stored inside a variable.

  • Function may or may not have a name.If the function doesn't have a name,then it is called anonymous function.

  • If the function is anonymous, the variable name acts as its function name.

Example

const display = function(num){
   console.log(num);
}
Enter fullscreen mode Exit fullscreen mode

Arrow Function

  • Arrow Functions allow a shorter syntax for function expressions.
const add = (a,b)=>{
  return a+b;
}
console.log(add(2,3));
Enter fullscreen mode Exit fullscreen mode

Top comments (0)