OneBite.Dev - Coding blog in a bite size

call a function with parameter in Javascript

Code snippet on how to call a function with parameter in Javascript

  function add(num1, num2 = 0) {
    return num1 + num2;
  }
  const total = add(2, 3);

This code uses a function named “add” to add two numbers. The first number is passed to the function as a regular argument (in this case it’s 2), and the second number is passed as a parameter with a default value of 0. The result of the function is stored in the “total” variable. Finally, the function is called with the two numbers (2 and 3) as arguments, and the result is stored in the “total” variable. The final result of the code is that the “total” variable holds the value 5, which is the sum of the two numbers.

javascript