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;

6 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
17 Likes

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
}

56 Likes

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] ]
6 Likes

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

1 Like

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

6 Likes

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’

1 Like

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.

5 Likes

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.

2 Likes

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.

1 Like

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')
2 Likes

Can anyone explain why the following block would require the {return num > 1000;}); format and not the typical arrow format “num => num > 1000” :

const greaterThan1000 = jumbledNums.findIndex(num => {
return num > 1000;
});

A lot of these examples in the “iterators” section of Intro to JavaScript include ‘return’ in the arrow function and I can’t seem to understand why it is necessary.

Thanks!

This is more of a comment than a problem. While it makes sense…it did come across a bit out of left field while your ambling along… it would have been cool to say something along the lines of HINT: Ternary Operators?! Remember those… This might be a cool spot for this bit of code you already learnt. Anyways Happy Coding everyone. And GO DODGERS!!

1 Like

and similarly:
return animal[0] === ‘s’;

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

This is just a type of if statement you have learnt in “if conditions.”

Hello,
findIndex() and filter() methods don’t actually print the words ‘true’ or ‘false’ to the console. From the instruction I thought they were going to be printed when I console.log the new arrays.

A post was split to a new topic: Isn’t it a bit redundant?