Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Scope JS Dates JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Data Types JS Errors JS Conventions JS References

JS Versions

JS Versions

JS HTML DOM

JS HTML DOM

JS Events

JS Events

JS Projects

JS Projects

JS Advanced

JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web APIs JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript Functions

Reusable Code Blocks

Functions are reusable code blocks designed to perform a particular task.

Functions are executed when they are called or invoked.

Functions are fundamental in all programming.

Why Use Functions?

Functions help you to:

  • Reuse code (write once, run many times)
  • Organize code into smaller parts
  • Make code easier to read and maintain

What Does a Function Look Like?

A function can be created with the function keyword, a name, and parentheses.

The code to run is written inside curly brackets.

Example

A one liner:

function sayHello() { return "Hello World"; }
or more common:
function sayHello() {
  return "Hello World";
}

Note

The function above does not do anything.

It has to be called first.


Functions Run When You Call Them

To run a function, you call it by using its name followed by parentheses like sayHello():

Example

function sayHello() {
  return "Hello World";
}

let message = sayHello();
Try it Yourself »

Note

Note that the value returned from the function can be stored in a variable.



JavaScript Function Syntax

function name( p1, p2, ... ) {
  // code to be executed
}

Functions are defined with the function keyword:

  • followed by the function name
  • followed by parentheses ( )
  • followed by brackets { }

Optional parameters are listed inside parentheses: ( p1, p2, ... )

Code to be executed is listed inside curly brackets: { }

Functions can return an optional value back to the caller.

Example

Function to multiply two numbers:

function multiply(a, b) {
  return a * b;
}
Try it Yourself »

Note

The function name follows the naming rules for variables.


A Function Can Be Used Many Times

A big benefit is that you can call the same function whenever you need it.

Example

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

let sum1 = add(5, 5);
let sum2 = add(50, 50);
Try it Yourself »

Function Input and Output

The most useful functions work like this:

  • Input - some parameters go in
  • Function - some work is done
  • Output - some value is returned

In the next three chapters, you will learn more about input and return values.


Common Mistakes

  • Forgetting to Call the Function

    Writing sayHello does not run the function. You must write sayHello().
  • Expecting Output

    If a function does not return a value, storing it in a variable will give undefined.

Why Functions?

Functions enable better code organization and efficiency.

With functions you can reuse code.

You can write code that can be used many times.

The same code, with different input, can produce different results.


Next Chapter

Next: Calling JavaScript Functions



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->