-->
Showing posts with label arrow function. Show all posts
Showing posts with label arrow function. Show all posts

Tuesday, July 15, 2025

JavaScript Tutorial: Learn Functions and Arrow Functions with Examples

 

JS Function programs collection

JS program to show the concept of arrow function


1. JS program to print hi using arrow function

<script>
s=()=>//same as function s()
{
  console.log("hi");
}
s();
</script>


2. JS program to print sum of two numbers using arrow function

<script>
sum=()=>//same as function sum()
{
  var a=30,b=20;
  return a+b;
}
console.log(sum());
</script>

Or

traditional way to use function

<script>
function sum()
{
  var a=30,b=20;
  return a+b;
}
console.log(sum());
</script>

3.JS program to print sum of two numbers using arrow function and passing parameters.

<script>
sum=(a,b)=>//means the function sum is accepting two parameters
{
  return a+b;
}
console.log(sum(12,13));
</script>


Or
<script>
sum=(a,b)=> a+b;//means the function sum is accepting two parameters
console.log(sum(12,13));
</script>


Note:We can not use return with a+b e.g. return a+b. Js does not support.
Use under braces. This is called implicit return. It means returning value without using return.