Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære What Are Functions? | Mastering Functions
Introduction to JavaScript
course content

Kursinnhold

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
What Are Functions?

Note
Definition

Functions are a feature in programming that lets us reserve a block of code to be executed later. This also enables us to execute that block of code multiple times with ease.

The basic syntax of defining a function is as follows:

js

Here function is the keyword used to define a function, and funcName represents the name of the function that we want to create.

Creating a function is more accurately referred to as "defining" a function. The code which defines a function is referred to as the "function definition" code.

Note
Note

The DRY (Don't Repeat Yourself) principle is a core programming concept that emphasizes minimizing code duplication. It encourages writing each piece of logic once and reusing it whenever necessary. This improves the code's readability and efficiency. Functions play an important role in adhering to this principle, as they allow us to eliminate any redundant code.

Following is an example of a function which draws a triangle in the console:

123456789
function drawTriangle() { console.log("*"); console.log("* *"); console.log("* * *"); console.log("* * * *"); console.log("* * * * *"); } drawTriangle();
copy

It's possible to execute a function more than once:

1234567891011
function drawTriangle() { console.log("*"); console.log("* *"); console.log("* * *"); console.log("* * * *"); console.log("* * * * *"); } drawTriangle(); drawTriangle(); drawTriangle();
copy
Note
Note

Executing a function is also sometimes referred to as calling a function. Similarly, a statement that executes a function (for example: myFunc()) is referred to as a Function Call.

It is recommended to name the functions meaningfully such that the name of the function accurately reflects the operation it performs.

1. What keyword is used to define a function in JavaScript?

2. What will be the output of the following code?

3. Which of the following is NOT true about functions?

question mark

What keyword is used to define a function in JavaScript?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

question mark

Which of the following is NOT true about functions?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 1

Spør AI

expand
ChatGPT

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

course content

Kursinnhold

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
3. Conditional Statements
4. Mastering Functions
5. Exploring Arrays
6. Discovering Loops

book
What Are Functions?

Note
Definition

Functions are a feature in programming that lets us reserve a block of code to be executed later. This also enables us to execute that block of code multiple times with ease.

The basic syntax of defining a function is as follows:

js

Here function is the keyword used to define a function, and funcName represents the name of the function that we want to create.

Creating a function is more accurately referred to as "defining" a function. The code which defines a function is referred to as the "function definition" code.

Note
Note

The DRY (Don't Repeat Yourself) principle is a core programming concept that emphasizes minimizing code duplication. It encourages writing each piece of logic once and reusing it whenever necessary. This improves the code's readability and efficiency. Functions play an important role in adhering to this principle, as they allow us to eliminate any redundant code.

Following is an example of a function which draws a triangle in the console:

123456789
function drawTriangle() { console.log("*"); console.log("* *"); console.log("* * *"); console.log("* * * *"); console.log("* * * * *"); } drawTriangle();
copy

It's possible to execute a function more than once:

1234567891011
function drawTriangle() { console.log("*"); console.log("* *"); console.log("* * *"); console.log("* * * *"); console.log("* * * * *"); } drawTriangle(); drawTriangle(); drawTriangle();
copy
Note
Note

Executing a function is also sometimes referred to as calling a function. Similarly, a statement that executes a function (for example: myFunc()) is referred to as a Function Call.

It is recommended to name the functions meaningfully such that the name of the function accurately reflects the operation it performs.

1. What keyword is used to define a function in JavaScript?

2. What will be the output of the following code?

3. Which of the following is NOT true about functions?

question mark

What keyword is used to define a function in JavaScript?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

question mark

Which of the following is NOT true about functions?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 1
Vi beklager at noe gikk galt. Hva skjedde?
some-alt