if else statement within a function is breaking function in javascript
Solution 1:
An if
block doesn't return anything. It's a statement, not an expression. So this structure is incorrect:
var x = if (something) { 1 } else { 2 }
You can use a ternary conditional operator, which is an expression, to evaluate to a value:
var x = something ? 1 : 2;
Or set the value within the if
block:
var x = 0;
if (something) {
x = 1;
} else {
x = 2;
}
But you can't mix the two approaches like that.