Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Returning Data from Functions | Mastering Functions
Introduction to JavaScript
course content

Contenu du cours

Introduction to JavaScript

Introduction to JavaScript

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

book
Returning Data from Functions

We can return any kind of value from a function using a return statement.

123456
function sum(a, b) { return a + b; } let result = sum(10, 15); console.log(result); // Output: 25
copy

General Syntax

The general syntax of a return statement is

js

Where <value> is optional. If no value is provided, the function returns undefined:

1234567
function exampleFunc() { console.log("In the Function"); return; } let result = exampleFunc(); console.log("Return value if nothing is returned:", result); // Output: undefined
copy

How It Works?

The return statement stops the execution of the function, and returns to the point in code the function where it was called. Therefore, any code after return is ignored:

1234567891011
function exampleFunc() { console.log(1); console.log(2); return true; // Execution stops here console.log(4); // Ignored console.log(5); // Ignored } console.log("Before Function Call"); console.log(exampleFunc()); // Output: true console.log("After Function Call");
copy

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

2. What happens if a function has a return statement with no value?

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

4. In the following code, what will be the value of result?

question mark

What will be the output of the following code?

Select the correct answer

question mark

What happens if a function has a return statement with no value?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

question mark

In the following code, what will be the value of result?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 9

Demandez à l'IA

expand
ChatGPT

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

course content

Contenu du cours

Introduction to JavaScript

Introduction to JavaScript

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

book
Returning Data from Functions

We can return any kind of value from a function using a return statement.

123456
function sum(a, b) { return a + b; } let result = sum(10, 15); console.log(result); // Output: 25
copy

General Syntax

The general syntax of a return statement is

js

Where <value> is optional. If no value is provided, the function returns undefined:

1234567
function exampleFunc() { console.log("In the Function"); return; } let result = exampleFunc(); console.log("Return value if nothing is returned:", result); // Output: undefined
copy

How It Works?

The return statement stops the execution of the function, and returns to the point in code the function where it was called. Therefore, any code after return is ignored:

1234567891011
function exampleFunc() { console.log(1); console.log(2); return true; // Execution stops here console.log(4); // Ignored console.log(5); // Ignored } console.log("Before Function Call"); console.log(exampleFunc()); // Output: true console.log("After Function Call");
copy

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

2. What happens if a function has a return statement with no value?

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

4. In the following code, what will be the value of result?

question mark

What will be the output of the following code?

Select the correct answer

question mark

What happens if a function has a return statement with no value?

Select the correct answer

question mark

What will be the output of the following code?

Select the correct answer

question mark

In the following code, what will be the value of result?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 4. Chapitre 9
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt