Can you explain return in javascript? - javascript

How is return used in javascript ?
var makeNoise = function() {
console.log("Pling!");
};
makeNoise();
// → Pling!
var power = function(base, exponent) {
var result = 1;
for (var count = 0; count < exponent; count++)
result *= base;
return result;
};
console.log(power(2, 10));
// → 1024
I am learning about functions in chapter 3 in eloquent javascript.

This is possible the best description ever. The return statement ends function execution and specifies a value to be returned to the function caller.
Read from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return
And I would recommend you visit www.codecademy.com they will help you learn faster and understand concepts easily

When a function is invoked, it begins execution with the first statement and ends when it hits the } that closes the function body. That causes the function to return control to the part of the program that invoked the function.
The return statement can be used to cause the function to return early. When return is executed, the function returns immediately without executing the remaining statements.

Related

Creating a Function that Looks through a Array [duplicate]

If I have the following for loop
for (var i = 0; i < SomeArrayOfObject.length; i++) {
if (SomeArray[i].SomeValue === SomeCondition) {
var SomeVar = SomeArray[i].SomeProperty;
return SomeVar;
}
}
Does the return statement stop the function's execution?
Yes, functions always end whenever their control flow meets a return statement.
The following example demonstrates how return statements end a function’s execution.
function returnMe() {
for (var i = 0; i < 2; i++) {
if (i === 1) return i;
}
}
console.log(returnMe());
Notes: See this other answer about the special case of try–catch–finally and this answer about how the forEach callback has its own function scope, so it will not break out of the containing function.
In most cases (including this one), return will exit immediately. However, if the return is in a try block with an accompanying finally block, the finally always executes and can "override" the return in the try.
function foo() {
try {
for (var i = 0; i < 10; i++) {
if (i % 3 == 0) {
return i; // This executes once
}
}
} finally {
return 42; // But this still executes
}
}
console.log(foo()); // Prints 42
This code will exit the loop after the first iteration in a for of loop:
const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];
for (const iterator of objc) {
if (iterator.name == 2) {
return;
}
console.log(iterator.name);// 1
}
the below code will jump on the condition and continue on a for of loop:
const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];
for (const iterator of objc) {
if (iterator.name == 2) {
continue;
}
console.log(iterator.name); // 1 , 3
}
The return statement stops a loop only if it's inside the function (i.e. it terminates both the loop and the function). Otherwise, you will get this error:
Uncaught SyntaxError: Illegal return statement(…)
To terminate a loop you should use break.
Yes, once the return statement is executed, the entire function is exited at that very point.
Just imagine what would happen if it did not and continued looping, and executing that return statement each time? It would invalidate it's meaning of returning a value when you think about it.
The answer is yes, if you write return statement the controls goes back to to the caller method immediately.
With an exception of finally block, which gets executed after the return statement.
and finally can also override the value you have returned, if you return inside of finally block.
LINK: Try-catch-finally-return clarification
Return Statement definition as per:
Java Docs:
a return statement can be used to branch out of a control flow block
and exit the method
MSDN Documentation:
The return statement terminates the execution of a function and
returns control to the calling function. Execution resumes in the
calling function at the point immediately following the call.
Wikipedia:
A return statement causes execution to leave the current subroutine
and resume at the point in the code immediately after where the
subroutine was called, known as its return address. The return address
is saved, usually on the process's call stack, as part of the
operation of making the subroutine call. Return statements in many
languages allow a function to specify a return value to be passed back
to the code that called the function.
"return" does exit the function but if you want to return large sums of data, you can store it in an array and then return it instead of trying to returning each piece of data 1 by 1 in the loop.

For loop issue, I have been trying to figure it out [duplicate]

If I have the following for loop
for (var i = 0; i < SomeArrayOfObject.length; i++) {
if (SomeArray[i].SomeValue === SomeCondition) {
var SomeVar = SomeArray[i].SomeProperty;
return SomeVar;
}
}
Does the return statement stop the function's execution?
Yes, functions always end whenever their control flow meets a return statement.
The following example demonstrates how return statements end a function’s execution.
function returnMe() {
for (var i = 0; i < 2; i++) {
if (i === 1) return i;
}
}
console.log(returnMe());
Notes: See this other answer about the special case of try–catch–finally and this answer about how the forEach callback has its own function scope, so it will not break out of the containing function.
In most cases (including this one), return will exit immediately. However, if the return is in a try block with an accompanying finally block, the finally always executes and can "override" the return in the try.
function foo() {
try {
for (var i = 0; i < 10; i++) {
if (i % 3 == 0) {
return i; // This executes once
}
}
} finally {
return 42; // But this still executes
}
}
console.log(foo()); // Prints 42
This code will exit the loop after the first iteration in a for of loop:
const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];
for (const iterator of objc) {
if (iterator.name == 2) {
return;
}
console.log(iterator.name);// 1
}
the below code will jump on the condition and continue on a for of loop:
const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];
for (const iterator of objc) {
if (iterator.name == 2) {
continue;
}
console.log(iterator.name); // 1 , 3
}
The return statement stops a loop only if it's inside the function (i.e. it terminates both the loop and the function). Otherwise, you will get this error:
Uncaught SyntaxError: Illegal return statement(…)
To terminate a loop you should use break.
Yes, once the return statement is executed, the entire function is exited at that very point.
Just imagine what would happen if it did not and continued looping, and executing that return statement each time? It would invalidate it's meaning of returning a value when you think about it.
The answer is yes, if you write return statement the controls goes back to to the caller method immediately.
With an exception of finally block, which gets executed after the return statement.
and finally can also override the value you have returned, if you return inside of finally block.
LINK: Try-catch-finally-return clarification
Return Statement definition as per:
Java Docs:
a return statement can be used to branch out of a control flow block
and exit the method
MSDN Documentation:
The return statement terminates the execution of a function and
returns control to the calling function. Execution resumes in the
calling function at the point immediately following the call.
Wikipedia:
A return statement causes execution to leave the current subroutine
and resume at the point in the code immediately after where the
subroutine was called, known as its return address. The return address
is saved, usually on the process's call stack, as part of the
operation of making the subroutine call. Return statements in many
languages allow a function to specify a return value to be passed back
to the code that called the function.
"return" does exit the function but if you want to return large sums of data, you can store it in an array and then return it instead of trying to returning each piece of data 1 by 1 in the loop.

Correct use of the counter in functions with array [duplicate]

If I have the following for loop
for (var i = 0; i < SomeArrayOfObject.length; i++) {
if (SomeArray[i].SomeValue === SomeCondition) {
var SomeVar = SomeArray[i].SomeProperty;
return SomeVar;
}
}
Does the return statement stop the function's execution?
Yes, functions always end whenever their control flow meets a return statement.
The following example demonstrates how return statements end a function’s execution.
function returnMe() {
for (var i = 0; i < 2; i++) {
if (i === 1) return i;
}
}
console.log(returnMe());
Notes: See this other answer about the special case of try–catch–finally and this answer about how the forEach callback has its own function scope, so it will not break out of the containing function.
In most cases (including this one), return will exit immediately. However, if the return is in a try block with an accompanying finally block, the finally always executes and can "override" the return in the try.
function foo() {
try {
for (var i = 0; i < 10; i++) {
if (i % 3 == 0) {
return i; // This executes once
}
}
} finally {
return 42; // But this still executes
}
}
console.log(foo()); // Prints 42
This code will exit the loop after the first iteration in a for of loop:
const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];
for (const iterator of objc) {
if (iterator.name == 2) {
return;
}
console.log(iterator.name);// 1
}
the below code will jump on the condition and continue on a for of loop:
const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];
for (const iterator of objc) {
if (iterator.name == 2) {
continue;
}
console.log(iterator.name); // 1 , 3
}
The return statement stops a loop only if it's inside the function (i.e. it terminates both the loop and the function). Otherwise, you will get this error:
Uncaught SyntaxError: Illegal return statement(…)
To terminate a loop you should use break.
Yes, once the return statement is executed, the entire function is exited at that very point.
Just imagine what would happen if it did not and continued looping, and executing that return statement each time? It would invalidate it's meaning of returning a value when you think about it.
The answer is yes, if you write return statement the controls goes back to to the caller method immediately.
With an exception of finally block, which gets executed after the return statement.
and finally can also override the value you have returned, if you return inside of finally block.
LINK: Try-catch-finally-return clarification
Return Statement definition as per:
Java Docs:
a return statement can be used to branch out of a control flow block
and exit the method
MSDN Documentation:
The return statement terminates the execution of a function and
returns control to the calling function. Execution resumes in the
calling function at the point immediately following the call.
Wikipedia:
A return statement causes execution to leave the current subroutine
and resume at the point in the code immediately after where the
subroutine was called, known as its return address. The return address
is saved, usually on the process's call stack, as part of the
operation of making the subroutine call. Return statements in many
languages allow a function to specify a return value to be passed back
to the code that called the function.
"return" does exit the function but if you want to return large sums of data, you can store it in an array and then return it instead of trying to returning each piece of data 1 by 1 in the loop.

Does Return terminates a Loop entirely before it finishes? [duplicate]

If I have the following for loop
for (var i = 0; i < SomeArrayOfObject.length; i++) {
if (SomeArray[i].SomeValue === SomeCondition) {
var SomeVar = SomeArray[i].SomeProperty;
return SomeVar;
}
}
Does the return statement stop the function's execution?
Yes, functions always end whenever their control flow meets a return statement.
The following example demonstrates how return statements end a function’s execution.
function returnMe() {
for (var i = 0; i < 2; i++) {
if (i === 1) return i;
}
}
console.log(returnMe());
Notes: See this other answer about the special case of try–catch–finally and this answer about how the forEach callback has its own function scope, so it will not break out of the containing function.
In most cases (including this one), return will exit immediately. However, if the return is in a try block with an accompanying finally block, the finally always executes and can "override" the return in the try.
function foo() {
try {
for (var i = 0; i < 10; i++) {
if (i % 3 == 0) {
return i; // This executes once
}
}
} finally {
return 42; // But this still executes
}
}
console.log(foo()); // Prints 42
This code will exit the loop after the first iteration in a for of loop:
const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];
for (const iterator of objc) {
if (iterator.name == 2) {
return;
}
console.log(iterator.name);// 1
}
the below code will jump on the condition and continue on a for of loop:
const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];
for (const iterator of objc) {
if (iterator.name == 2) {
continue;
}
console.log(iterator.name); // 1 , 3
}
The return statement stops a loop only if it's inside the function (i.e. it terminates both the loop and the function). Otherwise, you will get this error:
Uncaught SyntaxError: Illegal return statement(…)
To terminate a loop you should use break.
Yes, once the return statement is executed, the entire function is exited at that very point.
Just imagine what would happen if it did not and continued looping, and executing that return statement each time? It would invalidate it's meaning of returning a value when you think about it.
The answer is yes, if you write return statement the controls goes back to to the caller method immediately.
With an exception of finally block, which gets executed after the return statement.
and finally can also override the value you have returned, if you return inside of finally block.
LINK: Try-catch-finally-return clarification
Return Statement definition as per:
Java Docs:
a return statement can be used to branch out of a control flow block
and exit the method
MSDN Documentation:
The return statement terminates the execution of a function and
returns control to the calling function. Execution resumes in the
calling function at the point immediately following the call.
Wikipedia:
A return statement causes execution to leave the current subroutine
and resume at the point in the code immediately after where the
subroutine was called, known as its return address. The return address
is saved, usually on the process's call stack, as part of the
operation of making the subroutine call. Return statements in many
languages allow a function to specify a return value to be passed back
to the code that called the function.
"return" does exit the function but if you want to return large sums of data, you can store it in an array and then return it instead of trying to returning each piece of data 1 by 1 in the loop.

Javascript function for counting numbers within strings [duplicate]

If I have the following for loop
for (var i = 0; i < SomeArrayOfObject.length; i++) {
if (SomeArray[i].SomeValue === SomeCondition) {
var SomeVar = SomeArray[i].SomeProperty;
return SomeVar;
}
}
Does the return statement stop the function's execution?
Yes, functions always end whenever their control flow meets a return statement.
The following example demonstrates how return statements end a function’s execution.
function returnMe() {
for (var i = 0; i < 2; i++) {
if (i === 1) return i;
}
}
console.log(returnMe());
Notes: See this other answer about the special case of try–catch–finally and this answer about how the forEach callback has its own function scope, so it will not break out of the containing function.
In most cases (including this one), return will exit immediately. However, if the return is in a try block with an accompanying finally block, the finally always executes and can "override" the return in the try.
function foo() {
try {
for (var i = 0; i < 10; i++) {
if (i % 3 == 0) {
return i; // This executes once
}
}
} finally {
return 42; // But this still executes
}
}
console.log(foo()); // Prints 42
This code will exit the loop after the first iteration in a for of loop:
const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];
for (const iterator of objc) {
if (iterator.name == 2) {
return;
}
console.log(iterator.name);// 1
}
the below code will jump on the condition and continue on a for of loop:
const objc = [{ name: 1 }, { name: 2 }, { name: 3 }];
for (const iterator of objc) {
if (iterator.name == 2) {
continue;
}
console.log(iterator.name); // 1 , 3
}
The return statement stops a loop only if it's inside the function (i.e. it terminates both the loop and the function). Otherwise, you will get this error:
Uncaught SyntaxError: Illegal return statement(…)
To terminate a loop you should use break.
Yes, once the return statement is executed, the entire function is exited at that very point.
Just imagine what would happen if it did not and continued looping, and executing that return statement each time? It would invalidate it's meaning of returning a value when you think about it.
The answer is yes, if you write return statement the controls goes back to to the caller method immediately.
With an exception of finally block, which gets executed after the return statement.
and finally can also override the value you have returned, if you return inside of finally block.
LINK: Try-catch-finally-return clarification
Return Statement definition as per:
Java Docs:
a return statement can be used to branch out of a control flow block
and exit the method
MSDN Documentation:
The return statement terminates the execution of a function and
returns control to the calling function. Execution resumes in the
calling function at the point immediately following the call.
Wikipedia:
A return statement causes execution to leave the current subroutine
and resume at the point in the code immediately after where the
subroutine was called, known as its return address. The return address
is saved, usually on the process's call stack, as part of the
operation of making the subroutine call. Return statements in many
languages allow a function to specify a return value to be passed back
to the code that called the function.
"return" does exit the function but if you want to return large sums of data, you can store it in an array and then return it instead of trying to returning each piece of data 1 by 1 in the loop.

Categories

Resources