-3
const result = 2

function dothis() {
    const number = 5

    if (number < 0) {
      result == 0 ? console.log('error')
      return false
    }
}

Why is this code giving an "unexpected return" error?

3 Answers3

1

When using ternary condition you need to use the syntax condition?statement:statement. Thus, you are missing the third part, the statement, hence the name implies ternary(three parts).

var results = [];
function dothis() {
    const number = -1;
    if (number < 0) {
      results.length == 0 ? console.log('error'): console.log('no error');
      return false
    }
}
dothis();
Ankit Agarwal
  • 28,439
  • 5
  • 29
  • 55
1

I found out that your conditional if else shorthand is not completed, Else part is missing there.

function dothis() {
    const number = 5

    if (number > 0) {
      results.length == 0 ? console.log('error') : console.log ('success')
      return false
    }
}

this will complete your condition. Please let me know if you are trying to do something else.

Shashank Malviya
  • 366
  • 2
  • 11
0

You're missing :

results.length == 0 ? console.log('error') : console.log('success')
Jorgedl
  • 127
  • 3