Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Bootcamps Get Certified Upgrade Teachers Spaces Bootcamps
   ❮     
     ❯   

Basic JavaScript

JS TutorialJS SyntaxJS VariablesJS OperatorsJS If ConditionsJS LoopsJS StringsJS NumbersJS FunctionsJS ObjectsJS ScopeJS DatesJS Temporal DatesJS ArraysJS SetsJS MapsJS IterationsJS MathJS RegExpJS DestructuringJS Data TypesJS ErrorsJS DebuggingJS ConventionsJS ReferencesJS 2026JS Versions

JS HTML

JS HTML DOMJS EventsJS ProjectsNew

JS Advanced

JS FunctionsJS ObjectsJS ClassesJS AsynchronousJS ModulesJS Meta & ProxyJS Typed ArraysJS DOM NavigationJS WindowsJS Web APIsJS AJAXJS JSONJS jQueryJS GraphicsJS ExamplesJS Reference


JavaScript Functions

Functions are Code Blocks

Functions arereusable code blocks designed to perform a particular task.

Functionsare executed when they arecalled orinvoked.

Functions arefundamental in all programming languages.

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 thefunction keyword, aname, andparentheses.

The code to run is written insidecurly 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 likesayHello():

Example

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

let message = sayHello();
Try it Yourself »

Note

() means execute now.



JavaScript Function Syntax

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

Functions are defined with thefunction keyword:

  • followed by the functionname
  • followed byparentheses ( )
  • followed bybrackets { }

Thefunction name follows the naming rules for variables.

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

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

Functions canreturn an optional value back to the caller.

Example

Function to multiply two numbers:

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

Note

A function definition is not an executable statement.

It is not common to end afunction definition with a semicolon.

Semicolons are used to separate executable JavaScriptstatements.


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 »

Note

Note that values returned from functions can be stored in variables.


Local Variables

Variables declaredwithin a JavaScript function, becomeLOCAL to the function.

Local variables can only be accessed from within the function.

Example

// code here can NOT use carName

function myFunction() {
  let carName = "Volvo";
  // code here CAN use carName
}

// code here can NOT use carName
Try it Yourself »

Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.

Note

Local variables are created when a function starts, and deleted when the function is completed.


Functions Used as Variables

Functions can be used as variables, in all types of formulas, assignments, and calculations.

Example

Instead of using a variable to store the return value of a function:

let x = toCelsius(77);
let text = "The temperature is " + x + " Celsius";

You can use the function directly, as a variable value:

let text = "The temperature is " + toCelsius(77) + " Celsius";
Try it Yourself »

Why Functions?

Functions enablebetter code organization and efficiency.

With functions you canreuse the same code many times.

The same code, with different input, canproduce different results.


Function Input and Output

The most useful functions work like this:

  • Parameters - some values are sent to the function
  • Arguments - some values are received by the function
  • Function Code - some work is done inside the function
  • Return Output - some value is returned from the function

In the next chapters, you will learn more aboutinput andreturn values.


Next Chapter

Calling JavaScript Functions

  • Functions areexecuted when they arecalled orinvoked
  • You call a function by addingparentheses to its name like:name()



×

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 ourterms of use,cookies andprivacy policy.

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

-->
[8]ページ先頭

©2009-2026 Movatter.jp