Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 7.69

bookFunction Declarations

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 3
some-alt