What does ? true : false mean?

Would anyone be able to explain why the ?true : false was at the end of the code?

return animal[0] === ‘l’ ? true : false;

2 Likes

That is a control flow device known as a ternary expression. The term refers to three components.

condition ? true case : false case
//        ^           ^
//     ternary      colon
//     operator    separator
1 Like

Just wanted to share.

return animal[0] === ‘s’ ? true : false;

can be the same as writing,

if (animal[0] === ‘s’) {
return true
}
else {
return false
}

Hello:

I did the practive for .findIndex() where you see what number meets the condition you wrote. My question is what if there are more than one trues? Looks like it only picks the first item in the array that is true, but in my array, there are two true items. Is there a way you can identify both?

Thanks!

const jumbledNums = [25, 78, 5, 111, 9, 66, 43, 145, 12, 8];
const myNums = jumbledNums.findIndex(iMadeUpThis);

function iMadeUpThis(numnum) {
return numnum > 100;
};

console.log(myNums); // returned 3 which is 111

Not with this method, since it finds the first occurrence and returns its index.

We could use a filter to find all the values, then iterate over that list to find the indices of each of its members in the main array.

let bigNums = jumbledNums.filter(x => x > 100);
// [111, 145]

let bigNumsIndex = [];
bigNums.forEach(x => bigNumsIndex.push([x, jumbledNums.indexOf(x)]));
// [ [111, 3], [145, 7] ]
1 Like

Oddly the exercise only accepted
return animal[0] === 's' ? true : false;
as the correct answer even though
? true : false
literally has no meaning.

tried it, return animal[0] === 's'; is also accepted. Given the comparison results in a boolean value

Why would you need to include the ? true : false; expression?
Whether I have -
return animal[0] === ‘s’ ? true : false;
OR
return animal[0] === ‘s’;
when I log to the console -
console.log(foundAnimal);
console.log(startsWithS);
console.log(animals[7]);
console.log(animals[3]);
it outputs the same -
7
3
‘elephant’
‘seal’

We cannot speak for the author though one suspects learners may not yet be comfortable with ternary expressions, so this is an opportunity to see one at work, once more. Also, until a learner is comfortable with truthy and falsy, boolean literals are a good fit to make things obvious.

Any comparison will yield a boolean outcome, so as you observe,

return animal.charAt(0) === 's'

is a perfectly valid return statement as it returns a boolean.

  1. Let’s see if we can find the index of the first animal that starts with the letter 's' .
startsWithS = animals.findIndex(animal => animal.charAt(0) === 's');

That will return the index for 'seal', which is 3.

Thanks for the quick response…makes sense. I actually was trying to remember how to find the index of the first animal that begins with an ‘s’ so it is nice to see that in action.

1 Like

Hi,

I got stuck on the 2nd part. The solution was
const startsWithS = animals.findIndex(animal => {
return animal[0] === ‘s’ ? true : false;
});

Why is the last part (? true : false;) there?

Without speculating what the author’s intention was, it serves to demonstrate a ternary expression as a possible return value. One of true or false will be returned depending upon the outcome of the conditional.

Granted, since the a === b is already a boolean, which would return likewise, it is not as explicit as the ternary, for this example.

Why does the ternary expression return the index of the first element that is “true”?
Is this built in to the Javascript language to not return any false elements?

return animal[0] === ‘s’ ? true : false;
console.log(startsWithS); //3

Since it returns 3 and not ‘true’, I assume that the .findIndex method was performed on the WORD that was true, and not the word “true”. So does Javascript aways process elements or string that return ‘true’ and ignore elements that return ‘false’, unless you specify for it to look for ‘false’ elements? How would you do that? Would this work?:

return animal[0] === ‘s’ ? false : true;
Or
return animal[0] !== ‘s’ ? true: false;

We’re not searching for the literals, true or false.

animal => {
  return animal[0] === ‘s’ ? true : false;
}

The above is a predicate function that returns a boolean. When it returns true that means the condition has been met, and .findIndex then returns the index.

It’s simpler to see this if we do not use a ternary, which is unnecessary in such a simple case.

animals.findIndex(animal => animal.charAt(0) === 's')
1 Like