最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Checking for a value in an array with ternary operator - Stack Overflow

programmeradmin3浏览0评论

I'm a little confused here. Can someone explain to me why a ternary operator doesn't work here?

If I do this using regular if-statement, it works

function check(arr, el) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] === el) {
      return true;
    }
  }
  return false;
}

console.log(check([1, 2, 3, 4, 5], 3));

If I use a ternary operator, I get false for a truthy condition

function check(arr, el) {
  for (var i = 0; i < arr.length; i++) {
    return arr[i] === el ? true : false;
  }
}

console.log(check([1, 2, 3, 4, 5], 3));

For contrast, here is a simple ternary check function

function checkTernary(num1, num2) {
  return num1 === num2 ? true : false;
}

console.log(checkTernary(2, 3)); // false
console.log(checkTernary(3, 3)); // true

I suspect that checking for a false condition outside of the loop is the answer. As in, I am evaluating the entire loop for a false. But evaluating each iteration for a true??? I just need a clear explanation on this and a possible conversion to ternary if it can be done

I'm a little confused here. Can someone explain to me why a ternary operator doesn't work here?

If I do this using regular if-statement, it works

function check(arr, el) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] === el) {
      return true;
    }
  }
  return false;
}

console.log(check([1, 2, 3, 4, 5], 3));

If I use a ternary operator, I get false for a truthy condition

function check(arr, el) {
  for (var i = 0; i < arr.length; i++) {
    return arr[i] === el ? true : false;
  }
}

console.log(check([1, 2, 3, 4, 5], 3));

For contrast, here is a simple ternary check function

function checkTernary(num1, num2) {
  return num1 === num2 ? true : false;
}

console.log(checkTernary(2, 3)); // false
console.log(checkTernary(3, 3)); // true

I suspect that checking for a false condition outside of the loop is the answer. As in, I am evaluating the entire loop for a false. But evaluating each iteration for a true??? I just need a clear explanation on this and a possible conversion to ternary if it can be done

Share Improve this question asked Jun 15, 2019 at 14:51 LOTUSMSLOTUSMS 10.3k18 gold badges78 silver badges153 bronze badges 8
  • 2 You're returning false immediately, rather than waiting for the end of the loop. Consider looking into Array#some and Array#every – Niet the Dark Absol Commented Jun 15, 2019 at 14:53
  • Then again, in this case you're just re-inventing Array#contains – Niet the Dark Absol Commented Jun 15, 2019 at 14:53
  • @NiettheDarkAbsol interesting. Are you saying using every() to check the array instead of a for loop? – LOTUSMS Commented Jun 15, 2019 at 15:00
  • as soon as you return anything from your function, it stops executing. since the first element in the array is not the one being checked for, the ternary skips true and returns false and that's that. – JSONaLeo Commented Jun 15, 2019 at 15:09
  • 1 @LOTUSMS awesome! happy to help and good luck – JSONaLeo Commented Jun 15, 2019 at 15:43
 |  Show 3 more ments

4 Answers 4

Reset to default 2

Whenever the return statement is executed the function doesn't proceed further and returns a value.

In the first case the code will return false only if the condition is true if condition is false then it will proceed further. In the first case the function will return false after the end of the loop.

In the second example there is no condition to execute return so it will return in first iteration and function will not execute further.

A function can only return once, so we can't put them in a loop. But if you really want to use a ternary operation you can do it like this

function check(arr, el) {
    let check = false;
    for (var i = 0; i < arr.length; i++) {
        check = arr[i] === el ? true : check;
    }
    return check
}

but keep in mind that it will loop over all the elements in array even after it finds an equal element so an even better solution would be your first solution, which is

function check(arr, el) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] === el) {
      return true;
    }
  }
  return false;
}

arr[i] === el ? true : false is an expression which will be evaluated to a value, true or false.

return arr[i] === el ? true : false is a return statement which can only be executed once and will return value of the expression arr[i] === el ? true : false at the first time of loop.

The most important point is that any return statement can only be executed once, or zero times which means not be executed. Nothing else will affect this rule as far as I know.

One Liner with ES6

const checkTernary = (num1, num2) => num1 === num2;

console.log(checkTernary(2, 3)); // false
console.log(checkTernary(3, 3)); // true
发布评论

评论列表(0)

  1. 暂无评论