-->
Showing posts with label function in JS. Show all posts
Showing posts with label function in JS. Show all posts

Thursday, July 17, 2025

JavaScript Tutorial: Learn Functions and Arrow Functions with Examples

 

JS Function programs collection

JS program to show the concept of arrow function


1. Normal function to return an object
<script>
 function getUser () 
{
return{
//taking the braces in newline terminates the return so the
//followed statement does not work
  name: "Radhe krishna", 
  age: 25 
};
 }
document.write(JSON.stringify(getUser()));
//it converts the object into string with the help of stringify()
</script>


JS program using arrow function to return an object


<script>
 getUser=()=> 
{
return{
//taking the braces in newline terminates the return so the
//followed statement does not work
  name: "Radhe krishna", 
  age: 25 
};
 }
document.write(JSON.stringify(getUser()));
//it converts the object into string with the help of stringify()
</script>

Or


<script>
 getUser=()=> 
(
//() tells JavaScript: “This is  returning an expression.
  {
//taking the braces in newline terminates the return so the
//followed statement does not work
  name: "Radhe krishna", 
  age: 25 
}
 );
document.write(JSON.stringify(getUser()));
//it converts the object into string with the help of stringify()
</script>



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.

Saturday, June 28, 2025

JavaScript Program to use function to find sum of series– Function concept in JS with Examples

 

JS Function programs collection

JS program to show the concept of function

JS program to find sum of series using function  sum()

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>sum of series</title>

</head>

<body>

<h1>the sum is</h1>

<script type="text/javascript">

function sum()

{

var i,sum=0;

for(i=1;i<=100;i++)

{

sum+=i;

}

document.write(sum);

}

</script>

<script type="text/javascript">

sum();

</script>

</body>

</html>