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 == "") { 
    //something1 
                } else { //something2 }

I tried this, but is giving me an error:

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

Thanks!

You can use the ternary operator for that:

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

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
vs
var mysupervar = (input.value === '') ? something : somethingElse

Just my own little sanity check.

3 Likes

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;
1 Like

Thanks!

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; }
1 Like

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; }

vs

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

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

1 Like

Got it, thanks!!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.