What does ? true : false mean?

Thank you. This helped a lot.

hi , i know this topic is for 2019 but i found it interesting that why we need to write the ? true : false; ? i know it mean that if yes return true if not return false , but even if we don’t write it this wil get executed automatically, so why it is it needed here ?

What you are describing is known as a ternary expression. It gets its name from the three parts that make it up.

                 1            0
expression ? expression : expression
           ^             \
           |              binary separator
     ternary operator

The ternary operator tests the first expression for truthiness (a conditional). If the expression is truthy then the yield of the expression (value outcome) will be the expression under the 1, otherwise it will be the expression under the 0.

We see it presented as yielding true or false in your example, which is typical when the expression is being taught. In real practice we would never (at least I wouldn’t) write it like that.

return a === b ? true : false

when we could just as easily write,

return a === b

and get the same result.

The beauty of the ternary expression is it is a conditional expression that can be returned from a function.

const parity = x => x % 2 ? 'odd' : 'even'

console.log(parity(7)    //  odd
console.log(parity(6)    //  even

That is a practical usage example. Please do not make the mistake of thinking of this as just a short form of an if statement. While it might be, in a sense, it really is intended for simple, straight forward conditional in the form of an expression, not a statement. This is not the thing to reach for until after your if…else statement already works, and is simple enough to convert to a ternary.

An if…else statement has clearly defined structure and is easy to debug. A program full of ternaries could be a nightmare to debug, especially if they are complex. Yes, we can write some mighty pretty nested ternary expressions, but pretty isn’t always good. Good to play with, but not for production code. I only advise this because it is important that you focus on the logic of if…else, and get comfortable working with conditional statements in your programming. Make them the basis of your code logic. You might have to look this up:

Keep it imperative

and only when you are at the stage where expressive programming becomes the topic of the day. That is a ways down the road, and you’ll know what it means by the time you get there.

3 Likes

A function declaration can be written as such:

function formatter(s1, s2){
 // write your code here
 return // something
}

A shorter and more concise way of writing will be to use arrow functions

const formatter = (s1, s2) => { 
 // write your code here
 return // something
}

Note that the parenthesis surrounding the parameter (s) can be omitted if the supplied parameter count is 1, as such:

const formatter = s => {// write your code here} // 1 parameter

As for function accepting no parameters, the code will be as such:

const formatter = () => {// write your code here} // 0 parameter 

However, if the arrow function only consists of a single-line statement (most of the time being a return statement), the code will be as such:

const formatter = s => `This is a valid arrow function expression ${s}!!!`

If you somehow wrote your code like this: (without the opening and closing curly braces {} )

const formatter = s => s += "abc";
return `This is a valid arrow function expression ${s}!!!`;

The IDE will interpret the return statement line of code as invalid as it is not wrapped inside any function body whatsoever.

So, for your case above, both

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

and

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

are valid code to declare a function.

It so simple, I used it too!

1 Like