Search code examples
javascriptarraysecmascript-6ternary

Array.prototype.some not working with ternary operator


Why do the following two snippets not return the same value?

[1,2,3,4].some((el) => {
    if (el === 4) {
        return true;
    }
    else {
        return false;
    }
});

--> returns true

[1,2,3,4].some((el) => {
    el === 4 ? true : false;
});

--> returns false


Solution

  • Try this. You are missing return.

    var x=[1,2,3,4].some((el) => {
         return el === 4 ? true : false;
    });
    console.log(x);
    //Or you can do this
    var y=[1,2,3,4].some(el => el === 4);
    console.log(y);