define a function in javascript

To define a function in JavaScript, you can use the function keyword followed by the name of the function and parentheses containing any parameters. Here's an example:

index.tsx
function myFunction(parameter1, parameter2) {
  // Code to be executed
}
73 chars
4 lines

In this example, the function is named myFunction and it takes two parameters: parameter1 and parameter2.

You can then write the code that should be executed when the function is called inside the function body. For example:

index.tsx
function sayHello(name) {
  console.log("Hello, " + name + "!");
}

// Calling the function
sayHello("John");   // Output: Hello, John!
136 chars
7 lines

This function sayHello takes a name parameter and logs a greeting message to the console using that name.

You can also have functions that don't take any parameters or functions that return a value using the return statement.

related categories

gistlibby LogSnag