JavaScript Function Parameters

JavascriptWeb DevelopmentObject Oriented Programming

Pass different parameters while calling a function. These passed parameters can be captured inside the function and any manipulation can be done over those parameters. A function can take multiple parameters separated by comma.

Following is the code for implementing function parameters in JavaScript −

Example

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .sample {
      font-size: 18px;
      font-weight: 500;
   }
</style>
</head>
<body>
<h1>JavaScript Function Parameters</h1>
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
<h3>Click on the above buttons to see the function parameters </h3>
<script>
   let sampleEle = document.querySelector(".sample");
   test(param1, param2, param3, param4) {
      sampleEle.innerHTML = "The parameters for the function are <br>";
      Array.from(arguments).forEach((element, index) => {
         sampleEle.innerHTML += "Parameter " + index + " = " + element + "<br>";
      });
   }
   document.querySelector(".Btn").addEventListener("click", () => {
      test("Hello", "world", 22, 99);
   });
</script>
</body>
</html>

Output

On clicking the “CLICK HERE” button −


raja
Published on 08-May-2020 11:19:24
Advertisements