Open Playground
JS FunctionsLesson #43

JavaScript Function Parameters

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

Parameters (Function Input)

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

Parameters are listed inside the parentheses in the function definition.

Example

Example Code

javascript
Try it Yourself
function multiply(a, b) {

  return a * b;

}

let result = multiply(4, 5);

Functions with One Parameter

A function can have one parameter.

Examples

Example Code

javascript
Try it Yourself
function sayHello(name) {

  return "Hello " + name;

}

let greeting = sayHello("John");

Functions with Multiple Parameters

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

Example

Example Code

javascript
Try it Yourself
function fullName(firstName, lastName) {

  return firstName + " " + lastName;

}

let name = fullName("John", "Doe");

Parameters vs. Arguments

In JavaScript, function parameters and arguments are distinct concepts:

Parameters are the names listed in the function definition.

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

Learn More: > Function Arguments

Parameter Rules

JavaScript function definitions do not specify data types for parameters.

JavaScript functions do not perform type checking on the arguments.

JavaScript functions do not check the number of arguments received.


Incorrect Parameters

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

Example

Example Code

javascript
Try it Yourself
function toCelsius(fahrenheit) {

    return (5/9) * (fahrenheit-32);

}

let value = toCelsius();

Default Parameter Values

**ECMAScript 2015** allows function parameters to have default values.

The default value is used if no argument is provided.

Example

Example Code

javascript
Try it Yourself
function myFunction(x, y = 10) {

  return x + y;
}
myFunction(5);

Next Chapter > Function Return Values > A function can return a value back to the code that called it > The return statement is used to return a value from a function

Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
function multiply(a, b) {

  return a * b;

}


let result = multiply(4, 5);

Examples

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
function sayHello(name) {

  return "Hello " + name;

}


let greeting = sayHello("John");

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
function fullName(firstName, lastName) {

  return firstName + " " + lastName;

}


let name = fullName("John", "Doe");

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
function toCelsius(fahrenheit) {

    return (5/9) * (fahrenheit-32);

}



let value = toCelsius();

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
function myFunction(x, y = 10) {
 
  return x + y;
}
myFunction(5);