if statements vs shortened if statements [duplicate] - javascript

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.

Related

Merge Two Arrays and Values [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.

How do I get my For loop to not finish early but still return an array in a function? [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.

How to loop through an array of objects and see if values are empty [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.

Can't return a value within if statement in JavaScript

I have been stuck on this for a couple hours. I tested a recursive function in JavaScript, returned from an if statement but only the function execution context ends and the value I tried to return, was not assigned to the variable. It console.log's as undefined.
Here is my code, it's simple as I'm learning recursive functions:
function foo(i) {
// if i equals 5, return out of function
if (i === 5) {
console.log("We are now at 5");
return "Done";
} else {
console.log(i);
foo(++i);
}
}
let test = foo(0);
console.log(test);
I just simply don't understand why it doesn't return "Done" to the variable, yet if I put the return statement outside of the if/else, it returns how I intend. I tried other regular, simple, non recursive functions and they return fine within the if block.
The problem isn't with returning "Done", but with the recursive call. You just call it instead of returning it, so the returned value from the recursive call isn't returned "upwards". Just return it, and you should be OK:
function foo(i) {
// if i equals 5, return out of function
if (i === 5) {
console.log("We are now at 5");
return "Done";
} else {
console.log(i);
return foo(++i); // Here!
}
}

What is the point of the return in Javascript?

I've recently been learning javascript and came across the return statement or whatever it is. I've watched tutorials on it and I still don't get the point of it. Do I ever need it for anything and can someone please explain in detail what it does? Thanks
The return statement has multiple uses:
1) Return early from your function before the end of the function body. This is often within a branch of the code as in:
function whatever()
// do some things
if (err) {
console.log(err);
return err;
}
// other code here that will not execute if there was an err
}
2) Return a specific value back to the caller as in:
function add(a, b) {
return a + b;
}
var sum = add(3,4);
console.log(sum); // will show 7
The return statement in javascript can be used by itself to just exit the current function call and not return a specific value or it can be used to return a specific value.
If no return statement is present in a function, then the function will execute to the end of the function body and will return automatically at the end of the function body.
The only point of it is to send a value (string, number, or other values) from your function so that is can be used outside the function. Instead you can use global variables but that takes up more code, so it's a shortcut instead.
jfreind00 above said that you can use it to branch off early but that's what break. Here is the syntax:
function HiThere(something) {
if (something === true) {
break;
}
alert("Hi");
}
In this case, if something is true, then exit the function, if not then say hi.
Return would be like this:
function HiThere(something) {
if (something === true) {
return "Value to return: " + something;
}
alert("Hi");
}
In this case, if something is true, then exit the function and return Value to return: true, if not then say hi but don't return anything back.
Here's the most common use of the return statement:
document.getElementById("demo").innerHTML = myFunction(5,3); // Call a function
// to set the "demo" element to the value of: 5 * 3
function myFunction(a,b) {
return a * b;
}
<p id="demo"></p>
Use it to return a value. Your value could be "Yes", "false", "hello world" or "eat dirt." You can then run certain code based on the returned value.
Hope this helps!

Categories