-->

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>