3

I had a bit of a weird result in the javascript console. I was trying to look for an alternative (more readable) version of the ternary operator, just for fun. Typing:

{ if(3===4) {5} else {6} }

Evaluates in my console to 6, but for some reason, I can not assign it to a variable, so running:

let a = { if(3===4) {5} else {6} }

Does not let me store it to the variable directly. So my main question is, if this block is returning something, why can't I assign it?

user2662833
  • 1,005
  • 1
  • 10
  • 20
  • 1
    You can not “assign” an if statement to a variable, that makes no sense to begin with. If you want to do it like this, then you have to assign values to `a` _inside_ the if and else branch code blocks. – CBroe May 14 '18 at 12:21
  • @CBroe Whether or not it "makes sense" is language-dependent, e.g., we do it in Ruby all the time. – Dave Newton May 14 '18 at 12:31
  • Yes thats why I was asking, this is pretty common in some other languages. – user2662833 May 14 '18 at 12:52
  • The [ternary operator (`?:`)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) is an operator, [`if` is a statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else). Operators and statements are different concepts, with different purposes in a program. They cannot be interchanged. – axiac May 14 '18 at 12:59
  • Curious if you want to assign a function to a variable? This link does that and invokes later https://stackoverflow.com/questions/36664097/js-assigning-function-to-variable –  May 17 '18 at 02:42

4 Answers4

8

The fact that blocks (and other statements) return values isn't accessible to your code. Consoles can see the result, and it exists at a language specification level, but not in your code.

Your options are the conditional operator¹ (which is quite readable when you're used to it, but you've said you're looking for alternatives to it) or the if/else assigning to a in both parts:

let a;
if (3 === 4) {
    a = 5;
} else {
    a = 6;
}

Or you could use an inline function (IIFE):

let a = (() => { if (3 === 4} return 5 else return 6; })();

There is also a proposal being floated for "do expressions", which would look like this:

// Currently a proposal, not implemented in engines yet
let a = do { if (3 === 4) 5; else 6; };

That proposal is at Stage 1 of the process, so it may or may not progress, and if it progresses it could change markedly before doing so.


¹ Although you see "the ternary operator" a lot, the proper name is the conditional operator. It is a ternary operator (an operator accepting three operands), and currently JavaScript's only ternary operator, but that could change someday. :-)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Awesome answer, thanks for that! I'll wait a little before giving you this one. I suspected as much, but thought it would be cool to get a second opinion. Cheers – user2662833 May 14 '18 at 12:54
2

Use ternary operator, because you can't assign if statement:

let a = 3 === 4 ? 5 : 6;
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • 4
    Quote from original question: _“I was trying to look for an alternative (more readable) version of the ternary operator, just for fun.”_ – CBroe May 14 '18 at 12:21
1

The reason this doesn't work is because if is, as you pointed out, a statement. An assignment requires an expression holding a value to be assigned. A statement doesn't (per se) have a value--it simply performs some side-effect instead.

What you can use if you want to assign a value conditionally is the ternary conditional operator, which is an expression:

let a = (3 === 4 ? 5 : 6)

At this point you're probably wondering, why does the console print a value if a statement merely performs side-effects? This is because statements have a completion value, which for an if statement is the completion value of the corresponding branch, and for an expression statement (which is what the 5 and 6 inside the blocks are), is whatever the expression evaluates to. This is mainly useful for when you want to evaluate an expression in the console and see what it produces, without having to issue a console.log() each time you do.

FireFly
  • 394
  • 4
  • 15
0

It's not returning anything. if is not an expression, it's a statement; it has no value.

The point of the ternary operator was to provide a more readable version of this:

let a;
if (condition) {
    a = 1;
}
else {
    a = 2;
}
Máté Safranka
  • 4,081
  • 1
  • 10
  • 22