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 Function Parameters

Parameters and Arguments

Parameters allow you to pass (send) values to a function.

Parameters are listed inside the parentheses in the function definition.

Arguments are the real values passed to, and received by the function.

Example

function multiply(a, b) {
  return a * b;
}

let result = multiply(4, 5);
Try it Yourself »

Function parameters are the names listed in the function definition.

Function arguments are the real values passed to (and received by) the function.

In the example above:

  • a and b are parameters
  • 4 and 5 are arguments

The argument 4 is assigned to the parameter a.

The argument 5 is assigned to the parameter b.


Functions with One Parameter

A function can have one parameter.

Examples

function sayHello(name) {
  return "Hello " + name;
}

let greeting = sayHello("John");
Try it Yourself »
function toCelsius(fahrenheit) {
  return (5 / 9) * (fahrenheit - 32);
}

let value = toCelsius(77);
Try it Yourself »

Functions with Multiple Parameters

You can add as many parameters as you want, separated by commas.

Example

function fullName(firstName, lastName) {
  return firstName + " " + lastName;
}

let name = fullName("John", "Doe");
Try it Yourself »

The Order of Arguments Matters

Arguments are assigned to parameters in the order they appear.

Example

function subtract(a, b) {
  return a - b;
}

let x1 = subtract(10, 5);
let x2 = subtract(5, 10);
Try it Yourself »

The two calls above return different results because the order is different.



Incorrect Parameters

Accessing a function with incorrect parameters can return an incorrect answer:

Examples

function toCelsius(fahrenheit) {
  return (5/9) * (fahrenheit-32);
}

let value = toCelsius("John");
Try it Yourself »
function toCelsius(fahrenheit) {
  return (5/9) * (fahrenheit-32);
}

let value = toCelsius();
Try it Yourself »

Accessing a function without (), returns the function itself and not the result:

Example

function toCelsius(fahrenheit) {
  return (5/9) * (fahrenheit-32);
}

let value = toCelsius;
Try it Yourself »

Note

In the examples above:

toCelsius refers to the function itself.

toCelsius() refers to the function result.


Missing Arguments

If a function is called with fewer arguments than parameters, the missing values become undefined.

Example

function multiply(a, b) {
  return a * b;
}

multiply(4);

In the example above, b is undefined, so the result is NaN.

Note

A JavaScript function does not perform any checking on parameter values (arguments).


Parameter Rules

JavaScript function definitions do not specify data types for parameters.

JavaScript functions do not perform type checking on the passed arguments.

JavaScript functions do not check the number of arguments received.


Default Parameters

If a function is called with missing arguments (less than declared), the missing values are set to undefined.

Sometimes this is acceptable, but sometimes it is better to assign a default value to the parameter:

Example

function myFunction(x, y) {
  if (y === undefined) {
    y = 2;
  }
}
Try it Yourself »

Default Parameter Values

You can set a default value for a parameter.
This value is used if no argument is provided.

Example

function greet(name = "Guest") {
  return "Hello " + name;
}

greet();
greet("Anna");

Functions Can Be Called with Variables

Arguments do not have to be values. They can also be variables.

Example

let x = 5;
let y = 6;

function multiply(a, b) {
  return a * b;
}

multiply(x, y);

Common Mistakes

  • Confusing Parameters and Arguments

    Parameters are names. Arguments are values.
  • Forgetting the Order

    Arguments are assigned to parameters by position.
  • Missing Arguments

    Use default values to avoid undefined.

Quiz

What happens when a function is called with fewer arguments than parameters?


Next Chapter

Next: Returning Values from 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.

-->