This question already has answers here:
Javascript function fails to return element
(2 answers)
Closed 8 years ago.
I'm sure this is a noob issue but I'm a little confused. My understanding is that when a return statement is reached it should stop the function, however in the example below it doesn't seem to. Would someone please explain what's going on?
Fiddle
var hasTerm = function(obj,term){
$.each(jsonObject,function(key, value){
if (value == term){
console.log("conditional");
return true;
console.log("conditional after return");
};
if(typeof value == (Object || array)){
hasTerm(value,term);
}
});
return false;
}
The $.each is looping through all of the object collections passed in "jsonObject". Likely it is stopping on an object and then restarting on the next object. Does the console window ever show "conditional after return"?
Use "return false" to stop the iteration.
Related
This question already has answers here:
Recursive function returns undefined
(3 answers)
Function with forEach returns undefined even with return statement
(5 answers)
Closed 7 months ago.
I wrote a recursive function such as:
Note : you might not need to understand the nuts and bolts to help as it already looks like it does what I want
const rec = (arg_array, an_array, condition_state_out, state = 0) => {
if (state < condition_state_out) {
an_array.forEach((ele) => {
an_array.shift();
return rec([...arg_array, ele], an_array, condition_state_out, state + 1);
});
} else {
console.log(arg_array);
return arg_array;
}
};
When I run it,
const returned = rec([], an_array, condition_state_out, 0)
through the log, I see that it builds exactly what I want, so it is seemingly correct.
But when I check the returned object
console.log(returned)
It is undefined
I really can't figure out what is wrong as the complex part looks like it is working...
Many thanks for any help
This question already has answers here:
Is returning early from a function more elegant than an if statement?
(14 answers)
Is it good style (or more efficient) to "return early" at the top of a function?
(2 answers)
Invert "if" statement to reduce nesting
(25 answers)
Closed 3 years ago.
When writing javascript or PHP for that matter I use two ways to write if statements.
Return as soon as possible
Below I try to end the function as soon as possible. If the value is not what I expect, return it. The benefit I see here is that I don't need {} and I don't need to nest anything.
function hello(value = null) {
if(!value) return;
console.log("I'm still here!");
console.log("End of function");
}
Do something on match
In this case I don't return at all. Instead I do something if I get a "match". The upside here is that I don't need to return anything.
function hello(value = null) {
if(value) {
console.log("I'm still here!");
console.log("End of function");
}
}
So which is better? One of them or equally good?
This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 6 years ago.
I'm having a problem with my Javascript function, I'm not understanding something, just looking for some clarity.
I have a function:
function Test (array) {
if (array === []) {
return "the array is empty";
} else {
return array;
}
When I pass this function an empty array, it returns the empty array, completely skipping the first part of my if statement (this is the part I'm not understanding, why is it skipping that part? My understanding is that it would return my string statement at that point since the array I pass it, is in fact empty. If I remove the else statement, it returns "undefined".
NOTE! : I am aware that the solution to this problem is to set my "if" statement to compare the length of the array I pass it.
ex:
function Test (array) {
if (array.length === 0) {
return "the array is empty";
} else {
return array;
}
I'm just still not understanding why the first one doesn't work, and would really appreciate an explanation.
When you compare two objects in JavaScript, the comparison is asking "Are these objects the same object?", not "Are these objects identical?".
You are comparing two different empty arrays.
This question already has answers here:
return false the same as return?
(8 answers)
Closed 7 years ago.
I have two function for example :
function fn1(){
return false;
}
function fn2(){
return;
}
alert(fn1());
alert(fn2());
return will give you "undefined" and return false will give you boolean "false" what should i prefer and why ??
both will come out from the function but what to choose and what should not
Better as javascript standard point of you
You should return whatever you need.
If you don't need an specific answer just "return", if you want know the result of the function you must return a value (boolean or not).
This question already has answers here:
Why does typeof NaN return 'number'?
(21 answers)
Closed 9 years ago.
I just happen to stumble upon a code which checks the typeof of a varable passed to it just like this.
function myNaN(b){
if(typeof(b) == 'number'){
// execute some code
}
}
Whenever I call this function it works fine and passes the if condition if number is being passed.
However when I pass a NaN (which is the output of some other function) to this function the if condition returns true.
My question is it correct that typeof(NaN) == 'number' ? If so, why? Isn't it confusing?
Just try running console.log(typeof(NaN)); in browser console to see what I mean.
Yes, typeof(NaN) is number. You can check if the value is NaN specifically using the function isNaN.
Why don't you use the: "isNan("1234")" function ?
Here is some link if it helps: http://www.w3schools.com/jsref/jsref_isnan.asp