Syntax

JavaScript Define a Function Syntax

In JavaScript, define a function: defines a named function — function add(a, b) { return a + b; }

Quick Answer

Syntax
function add(a, b) { return a + b; }
JavaScriptSyntax

Define a Function

Code example
function add(a, b) { return a + b; }

Defines a named function.

Learn More

How to define a function in JavaScript

  1. Open your JavaScript file or editor.
  2. Copy the example, then replace its sample names and values for your code.

JavaScript Define a Function Syntax

Use function add(a, b) { return a + b; }.

Related Syntax References

Frequently asked questions

What is the JavaScript syntax for define a function?

Use this pattern: function add(a, b) { return a + b; }

Can I copy this JavaScript code example?

Yes. Use Copy code and adapt the example to your program.

Is this JavaScript syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify