create function in javascript with paramaters in javascript

To create a function in JavaScript with parameters, you can simply declare the parameter names inside the parentheses of the function declaration. Here is an example:

index.tsx
function greet(name) {
  console.log(`Hello, ${name}!`);
}

greet('John'); // Output: Hello, John!
99 chars
6 lines

In the example above, we have declared a function called greet that takes a single parameter called name. Inside the function, we are logging a string to the console using template literals.

When we call the greet function and pass in the argument 'John', the function will log the message "Hello, John!" to the console.

You can also define multiple parameters, separated by commas, like this:

index.tsx
function sum(a, b) {
  return a + b;
}

console.log(sum(2, 3)); // Output: 5
77 chars
6 lines

In this example, the sum function takes two parameters a and b, and returns their sum, which is then logged to the console.

related categories

gistlibby LogSnag