If inside var?


Hello fast question, its possible to use if inside var?

I want to do:
when x field is empty do this, and if not do this. I need to run the same IF multiple times so I want to use some kind of var reference and use it for all the cases.

Actually i know how to do my if comprobation:

if (input.value == "") { 
                } else { //something2 }

I tried this, but is giving me an error:

var mysupervar = {
  if (input.value == "") { //something1 
                  } else { //something2 }



You can use the ternary operator for that:

var mysupervar = input.value === '' ? something : somethingElse


Though for the sanity of people trying to read it, you might want to wrap your test in parenthesis :wink:


You mean because the // is affecting also the } right?

Yeah in my code test it's not like just there did the //something

Btw can't edit the post anymore to fix it.


No i was referring to @m3g4p0p 's answer (which is the right answer)
var mysupervar = input.value === '' ? something : somethingElse
var mysupervar = (input.value === '') ? something : somethingElse

Just my own little sanity check.


Thank you!

What if i don't want do anthing in the first something and only on the somethingElse? like this:

var mysupervar = input.value === '' ? //nothing : somethingElse

Maybe this?

var nothing = {}
var mysupervar = input.value === '' ? nothing : somethingElse

Edit: Does not need var always ; ?


well you're defining a variable. so there will ALWAYS be 'somethingElse'. You can hand it null if you want, but the variable has to be assigned SOMETHING.
Incidentally, convention would be to define the test the other way around in this case, because the 'else' is the optional part.

var mysupervar = (input.value !== "") ? input.value : null;



Actually, I did this and worked.

var nothing = {}
var mysupervar = (input.value === '') ? nothing : something

Can you explain to me the differences between mine expression and yours?

Or i should use this: ??

var mysupervar = (input.value === '') ? null : something


Well, there are two differences, really.

1: You're actually assigning 'nothing' as SOMETHING. Specifically, an empty object.
An empty object is still an Object. You can do things like {}.hasOwnProperty("a") and get a sensible value out. You can assign things to properties of an empty object. nothing.athing = "wark" still works.

2: You're doing the negative as the actual result. Consider how you would revert this back to if..then..else. What you've tried to write is:

else(input.value === "") { var mysupervar = something; }

Which makes no sense because there's no if.
If you wanted to write it in full form, you'd have to write:

if (input.value !== "") { var mysupervar = something; }


Hmmm, maybe then I did the wrong question.

I should use IF/else to try to do something only when the input.value is not empty? I don't need to do anything if its empty, just run something when it's not.

Maybe there's a better expression to do that? Thanks for your great information.


No, if is the appropriate way of checking if (see how the language construct came to be?) the value is empty.
Perhaps its easier to see what i mean if I write it out in sentence form.

"I want to check if the input value is empty; if it is, do nothing, if it isnt, do something"

You can say the same thing, but easier, by simply saying:

"I want to check if the input value is not empty; if it is, do something"

Both sentences mean the same thing.

In programming speak, those two sentences are expressed in long form as:

if (input.value === "") { }
else { something; }


if(input.value !== "") { something; }

or in ternary form, where they are almost identical:
(input.value === "") ? null : something;
(input.value !== "") ? something : null;


Got it, thanks!!