How to Create a New Function in JavaScript?

Estimated read time 1 min read

You can create a new function in JavaScript using the function keyword. Here’s an example:

function greet(name) {
  console.log("Hello, " + name + "!");

greet("John"); // Output: Hello, John!

In this example, the function greet takes a single argument name, and prints a greeting to the console using that name. The function is invoked by calling it with a specific argument, in this case "John".

You can also create a function using the function expression syntax, like this:

const greet = function(name) {
  console.log("Hello, " + name + "!");

greet("Jane"); // Output: Hello, Jane!

In this example, the function is assigned to a variable greet, so you can invoke it just like in the previous example.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply