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.
+ There are no comments
Add yours