2

Can someone give me some insight as to why the first 2 functions return undefined instead of a boolean?

https://jsfiddle.net/wjf2gr9d/

const array1 = [1, 2, 3];

const test1Result = test1();
const test2Result = test2();
const test3Result = test3();

console.log(test1Result);
console.log(test2Result);
console.log(test3Result);


function test1() {
    return array1.forEach(x => 2 === x);
}

function test2() {
    const found = array1.forEach((x, index) => {
        if (2 === x) {
            return true;
        }
        return false;
    });

    return found;
}

function test3() {
    const maybeTeam = array1.find(x => 2 == x);

    return (maybeTeam) ? true : false;
}
1
  • forEach return undefined Commented Dec 5, 2018 at 6:57

5 Answers 5

4

If you check this check return value section it returns undefined.

enter image description here

Foreach doesn't have any return types, You can use some. If you must use foreach then you can take temp variable and change it inside foreach like I did in test2

const array1 = [1, 2, 3];

const test1Result = test1();
const test2Result = test2();
const test3Result = test3();

console.log(test1Result);
console.log(test2Result);
console.log(test3Result);


function test1() {
    return array1.some(x => 2 === x);
}

function test2() {
    var found = false;
    array1.forEach((x, index) => {
        if (2 === x) {
            found = true;
        }
       
    });

    return found;
}

function test3() {
    const maybeTeam = array1.find(x => 2 == x);

    return (maybeTeam) ? true : false;
}
Credits: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Sign up to request clarification or add additional context in comments.

Comments

2

forEach always returns undefined; return <arr>.forEach will always result in undefined being returned, no matter the logic in the forEach. If you want to check whether any item in an array passes a particular test, you should use .some instead:

const array1 = [1, 2, 3];

const test1Result = test1();

console.log(test1Result);
function test1() {
    return array1.some(x => 2 === x);
}

Comments

1

Explanation

When a function is not having explicit return in it. by default javascript function return undefined for that function.

For reference just look at the example below. Here inner console.log('hello') is printing hello but since return implicit return value from inner console.log() is undefined so outer console.log() prints undefined.

console.log(console.log('Hello'))

Well i will suggest you look the changes.You will understand why it is so.

const array1 = [1, 2, 3];
test1();
test2();
console.log(test3());

function test1() {
    let a = array1.forEach(x => 2 === x);
    console.log(a);
    return a;
}

function test2() {
    const found = array1.forEach((x, index) => {
        if (2 === x) {
            return true;
        }
        return false;
    });

    console.log(found);
    return found;
}

function test3() {
    const maybeTeam = array1.find(x => 2 == x);

    return (maybeTeam) ? true : false;
}

Comments

1

Array#forEach does not return a special value, depending on the inner return value from the callback. It just iterates all items.

Array#some or its counterpart Array#every returns early, depending on the return value of the callback.


While you are using ES6, you could take Array#includes, which returns a boolean, if the handed over values is part of the array.

const array1 = [1, 2, 3];

console.log(array1.includes(2)); // true

Comments

0

forEach return undefined. what you want to use in your case is array.includes

const array1 = [1, 2, 3];
function test1() {
    return array1.includes(2);
}

const test1Result = test1();


console.log(test1Result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.