Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Question on javascript syntax regarding: condition ? <code if true> : <code if false>

How can I add multiple lines of code for this? I keep getting an error. This works:

  isChecked ?
    console.log('tru'):
    console.log('false')

But when I try to add multiple lines of code, it throws a syntax error of unexpected identifier:

  isChecked ?
    console.log('tru')
    console.log('second line'):
    console.log('false')

Is there a way to add multiple lines of code for each conditional? Or do I have to squeeze it all into 1?

Thanks

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Cameron Childres and Eric Peppler. Just adding my two cents though this was a great answer. You can imitate an if/else if/ else using ternaries, but they would need to be nested ternaries. A ternary by itself only contains what happens if it's true, and what happens if it's false. So to mimic it, you would need to put the false part as yet another condition which is a ternary.

Consider the following:

num1 = 15;
num2 = 25;

const getResult = (x) => {
  result = x > 20 ? "more than 20" : x > 10 ? "more than 10" : x >= 0 ? "zero or greater" : "less than 0";
  return result;
}

console.log(getResult(num1));
console.log(getResult(num2));
console.log(getResult(-3));

Output:

more than 10
more than 20
less than 0 

However, I think for the sake of clarity I probably wouldn't ever go above one nested ternary. Just my two cents :smiley:

But yes, in short, there is only one statement per section of the ternary and no more. Any more would need to be abstracted out into a function as Cameron pointed out :smiley:

Hope this is useful in some way! :sparkles:

Cameron Childres
Cameron Childres
11,819 Points

Good point! I assumed the goal was to have multiple statements per condition, didn't think to mention the possibility of multiple conditions.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Cameron Childres I couldn't exactly tell if they meant as in if else sort of thing or just simply two statements in one "section" of the ternary, so to be on the safe side, I commented on both :smiley:

Cameron Childres
Cameron Childres
11,819 Points

Hey Eric,

I don't believe the ternary operator can handle multiple statements per condition like that. You could pack them in to a function and call that function if you like:

function twoLines() {
    console.log('tru');
    console.log('second line');
}

isChecked ? twoLines()
    : console.log('false');

Depending on the use though it may be more appropriate to go with an "if-else".

Got it, thanks!