Iterators starts with 's'


#1

https://www.codecademy.com/courses/learn-javascript-iterators/lessons/javascript-iterators/exercises/find-index?action=resume_content_item&course_redirect=introduction-to-javascript

const startsWithS = animals.findIndex(animal => {
return animal[0] === ‘s’ ? true : false;

i understand what this does, but not understanding how it works, could someone explain the ? true :false


#2

This is known as a ternary expression. Ternary means, having three parts.

It is akin to an if..else statement.

condition or state ? expression if true : expression otherwise
                   ^                    ^
                ternary            case separator
               operator
var temp = 0;
console.log(temp > 32 ? "Above freezing" : "Below freezing");
// <- Below freezing

Comment

For this particular problem a complex return is overkill.

console.log(animals.findIndex(animal => animal == 'elephant'));
// <- 7
console.log(animals.findIndex(animal => animal.charAt(0) === 's'));
// <- 3