2

I have a problem. I will only declare a variable if that satisfies my condition.

if(//some condition1)
var some1=1;
else if(//some condition1)
var some2=2;

Now

var some3=some1+some2;

if two conditions are met, it is okay to have sum in some3;

if only one condition is met, I want to have either some1 or some2 in some3

How can I achieve this?

Note: Thanks for answers But i want to declare variables only if that condition is met. Is there any other solution?

Lonely
  • 613
  • 3
  • 6
  • 14

11 Answers11

3
var some3=0;

if(//some condition1){
  var some1=1;
  some3 += some1;
}else if(some condition2){
  var some2=2;
  some3 += some2;
}

EDIT: A better option :

var some3=0;           
if(some condition1){
   some3 += 1;
}else if(some condition2){
   some3 += 2;
}

and moreover both conditions wont meet at same time, so, if you want to you can do like below also

var some3=0;           
if(some condition1){
   some3 += 1;
}
if(some condition2){
   some3 += 2;
}
Deepanshu Goyal
  • 2,738
  • 3
  • 34
  • 61
  • @Deepenshu question says variable should be declared only if condition is true – Deepak Ingole Aug 01 '13 at 08:58
  • Good logic, but you are re-declaring your variables. Could also improve the formatting a bit – musefan Aug 01 '13 at 09:00
  • @Deepanshu Please check question.It says **declare variables only if that condition is met** – Deepak Ingole Aug 01 '13 at 09:03
  • 3
    no need to declare some1 and some2 here. you can just directly add it to some3. – artsylar Aug 01 '13 at 09:10
  • I think the OP means `defined` and `undefined` rather than `declared` and `undeclared`. With `var some3;` I declare the variable but it is still undefined. Declaring the variable (even inside the if) will declare it locally for the full function scope. – devnull69 Aug 01 '13 at 09:12
  • @artsylar you don't know if OP needs the separate values later on – basilikum Aug 01 '13 at 09:14
  • well now I have no idea what is the requirement... what i thought should be the best , I typed it down !!! because checking the variable for undefined and stuff is just so many lines of code for nothng – Deepanshu Goyal Aug 01 '13 at 09:15
  • Your code is fine ... the point is that the OP didn't know that a) undefined is not the same as undeclared and b) that declaring and defining a variable later in the scope will result in it being declared earlier and defined later as described by basilikum – devnull69 Aug 01 '13 at 09:17
2

First of all, you can't declare your variables only if a certain condition is satisfied. JavaScript has function scope which means, that no matter where in your code you write var some1, it will always be hoisted up, to the top of the function scope:

function A() {
   if (...) {
       var a = 1;
   }
}

will be interpreted as:

function A() {
   var a;
   if (...) {
       a = 1;
   }
}

That being said. I think to solve your problem you just need to initialize your variables with 0:

var some1 = 0,
    some2 = 0,
    some3;

if(//some condition1)
    some1 = 1;
else if(//some condition1)
    some2 = 2;

some3 = some1 + some2;

EDIT

Actually, there IS a way to to declare variables only when a condition is satisfied:

if (...) {
    eval("var x = 1;");
}

But I don't really see any need for a "hack" like that and wouldn't advice that method at all.

basilikum
  • 10,378
  • 5
  • 45
  • 58
1

Initialize both variables to 0 first.

var some1 = 0, some2 = 0;
if (condition1) {
    some1 = 1;
}
if (condition2) {
    some2 = 2;
}
var some3 = some1 + some2;

Since you want to allow both conditions to be met and set both variables, you should not use else if. Just two separate if statements.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1
if(//some condition1) {
    var some1 = 1;
} else if(//some condition1) {
    var some2 = 2;
}

var some3 = 0;

if(some1 !== undefined) {
    some3 += some1;
}
if(some2 !== undefined) {
    some3 += some2;
}
Virus721
  • 8,061
  • 12
  • 67
  • 123
1

How about an object:

var obj = {};
if(//conditon){
   obj.some1 = 1;
}
else if(//condition){
   obj.some2 = 1;
}

var sum3 = 0;
for(var key in obj){
    sum3 += obj[key];
}
Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57
0

why not just declare the variable first and then just set their values when it meets the conditions

var some1 = 0;
var some2 = 0;

if (//some condition)
{
  some1 = 1;
}
if (// some condition)
{
  some2 = 2;
}

var some3 = some1 + some2;
artsylar
  • 2,648
  • 4
  • 34
  • 51
0

Declare all the variables in the begin. Initialize them to 0. If the condition is met, change the variable value. Then, when you make the sum, any value plus 0 remains the same. Or if the condition was met, the value won't be 0.

Oscar
  • 13,594
  • 8
  • 47
  • 75
0

If you need to use that model, you could just use a fourth variable.

var some0 = 0
if(/*some condition1*/) {
var some1=1;
some0 = some1 + some3
}
else if(/*some condition1*/) {
var some2=2;
some0 = some2 + some3
}
some3=some0
Paul
  • 3,634
  • 1
  • 18
  • 23
0
var some3 = 0; //Assingning global var.
if(//some condition1)
  var some1=1;
else if(//some condition1)
  var some2=2;

In Conditions

if(some1 != null && some2 != null )
  some3=some1+some2;
else if(some1 != null && some2 == null)
  some3=some1;
else if(some1 == null && some2 != null)
  some3=some2;
else if(some1 == null && some2 == null)
  some3=some3;

Then

alert(some3);
Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
0

How about:

var some3 =
(2 > 4      ? 10:0) +
(5 < 8      ? 20:0) +
(10 == 11   ? 30:0) +
('a' < 'b'  ? 40:0) ;

I admit this does not look pretty.

S.D.
  • 29,290
  • 3
  • 79
  • 130
0

you an use the

if(//some condition1) var some1=1;

else if(//some condition1) var some2=2;

and then try catch statements like in my jsfiddle

try{

 var some3=some1+some2;

}
catch (e){
    try{
      var some3=some1;
      document.getElementById("test").innerHTML= some3;

    }catch (e){
        try{
           var some3=some2 ;
           document.getElementById("test").innerHTML= some3;

        }
        catch (e){
        document.getElementById("test").innerHTML="Something went wrong!!!!or no condition satisfied";
        }
    }
}
Andreas
  • 970
  • 18
  • 28
  • (The use of exceptions is inefficient, because of the overhead of throwing exceptions, but I was just giving an extra option form the other answers.) more info about the side effects of try catch in an [old question](http://stackoverflow.com/questions/3217294/javascript-try-catch-performance-vs-error-checking-code). In order to answer that question @jon-j created this [fiddle](http://jsfiddle.net/Mct5N/) which proves this. – Andreas Aug 01 '13 at 12:01