Rod
Rod

Reputation: 15455

Array forEach not returning boolean

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;
}

Upvotes: 2

Views: 7906

Answers (5)

Nina Scholz
Nina Scholz

Reputation: 386660

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

Upvotes: 1

Code Maniac
Code Maniac

Reputation: 37745

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;
}

Upvotes: 1

Just code
Just code

Reputation: 13801

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

Upvotes: 4

Amit Wagner
Amit Wagner

Reputation: 3264

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);

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370929

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);
}

Upvotes: 2

Related Questions