Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Defining Functions in JavaScript | Functions in JavaScript
Introduction to JavaScript

bookDefining Functions in JavaScript

To create a new function, use the function keyword. Provide a name for the function and define any arguments it should accept:

function functionName(arguments) {
  // code block
}

The structure of a function definition includes:

  • The function keyword;
  • The function name, typically in camelCase like variables;
  • Arguments enclosed in parentheses ();
  • A code block within curly brackets {}.

Arguments

Functions have their own data storage space. Arguments are values passed to a function, which the function uses as variables. These arguments cease to exist when the function finishes executing.

To define arguments, assign a name to each one:

// Function without arguments
function first() {}

// Function with one argument
function second(argument) {}

// Function with multiple arguments
function third(argument1, argument2, argument3) {}

If a function accepts multiple arguments, separate them with commas (,).

Note

Arguments are used like variables inside the function's code block.

Function calling

To use a function, call it using parentheses () and provide the necessary arguments if the function expects any:

1234567891011
// Function Definition function func(arg) { console.log("Argument received:", arg); console.log("Argument type:", typeof arg); } // Function Calling func(1); func(2521); func("Rabbit"); func(true);
copy
question mark

Which of the following correctly defines a function that takes two arguments?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 2.33

bookDefining Functions in JavaScript

Swipe to show menu

To create a new function, use the function keyword. Provide a name for the function and define any arguments it should accept:

function functionName(arguments) {
  // code block
}

The structure of a function definition includes:

  • The function keyword;
  • The function name, typically in camelCase like variables;
  • Arguments enclosed in parentheses ();
  • A code block within curly brackets {}.

Arguments

Functions have their own data storage space. Arguments are values passed to a function, which the function uses as variables. These arguments cease to exist when the function finishes executing.

To define arguments, assign a name to each one:

// Function without arguments
function first() {}

// Function with one argument
function second(argument) {}

// Function with multiple arguments
function third(argument1, argument2, argument3) {}

If a function accepts multiple arguments, separate them with commas (,).

Note

Arguments are used like variables inside the function's code block.

Function calling

To use a function, call it using parentheses () and provide the necessary arguments if the function expects any:

1234567891011
// Function Definition function func(arg) { console.log("Argument received:", arg); console.log("Argument type:", typeof arg); } // Function Calling func(1); func(2521); func("Rabbit"); func(true);
copy
question mark

Which of the following correctly defines a function that takes two arguments?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 6. ChapterΒ 2
some-alt