Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Function Declarations | Understanding and Writing Functions
Functions in JavaScript

bookFunction Declarations

To begin working with functions in JavaScript, you need to understand the syntax for declaring them. A function declaration starts with the function keyword, followed by the name you choose for your function. After the name, you use parentheses () to enclose any parameters the function might take. The body of the function is enclosed in curly braces {}. Inside the curly braces, you write the code that will run whenever the function is called. Here is a step-by-step breakdown:

  • Write the function keyword to tell JavaScript you are creating a function;
  • Choose a descriptive name for your function and write it immediately after the function keyword;
  • Add parentheses after the name—these can include parameter names if your function will use input values;
  • Open a curly brace to start the function body;
  • Write the statements you want the function to perform;
  • Close the curly brace to end the function body.
1234
function addAndLog(a, b) { const sum = a + b; console.log("The sum is: " + sum); }
copy

When naming your functions, always follow clear and consistent naming conventions. Function names should be descriptive and use lower camel case, which means starting with a lowercase letter and capitalizing the first letter of each subsequent word, like calculateTotal or sendEmail.

Avoid starting function names with numbers or special characters, and do not use spaces. Good function names describe what the function does, making your code easier to read and maintain. Stick to verbs or verb phrases, since functions usually perform actions.

question mark

Which of the following are valid JavaScript function declarations? Select all that apply.

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 3

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 7.69

bookFunction Declarations

Glissez pour afficher le menu

To begin working with functions in JavaScript, you need to understand the syntax for declaring them. A function declaration starts with the function keyword, followed by the name you choose for your function. After the name, you use parentheses () to enclose any parameters the function might take. The body of the function is enclosed in curly braces {}. Inside the curly braces, you write the code that will run whenever the function is called. Here is a step-by-step breakdown:

  • Write the function keyword to tell JavaScript you are creating a function;
  • Choose a descriptive name for your function and write it immediately after the function keyword;
  • Add parentheses after the name—these can include parameter names if your function will use input values;
  • Open a curly brace to start the function body;
  • Write the statements you want the function to perform;
  • Close the curly brace to end the function body.
1234
function addAndLog(a, b) { const sum = a + b; console.log("The sum is: " + sum); }
copy

When naming your functions, always follow clear and consistent naming conventions. Function names should be descriptive and use lower camel case, which means starting with a lowercase letter and capitalizing the first letter of each subsequent word, like calculateTotal or sendEmail.

Avoid starting function names with numbers or special characters, and do not use spaces. Good function names describe what the function does, making your code easier to read and maintain. Stick to verbs or verb phrases, since functions usually perform actions.

question mark

Which of the following are valid JavaScript function declarations? Select all that apply.

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 3
some-alt