0

Possible Duplicate Way to differentiate between declared variable vs undeclared

I want to explain clearly my problem here. I have this condition.

if(//condition1)
  var x1 = 1;
else if(//condition2)
  var x2 = 2;
else if(//condition3)
  var x3 = 3;

This way I can have only one variable declared. Now I want to print the variable which is declared. How can I do that?

This is a more realistic example of what I am trying to do:

var d_pattern=/d{1}/; 
var dd_pattern=/d{2}/; 
var ddd_pattern=/d{3}/; 
var dddd_pattern=/d{4}/; 

if(dddd_pattern.test(formatString)) 
    var dddd=this.getDayName(); 
else if(ddd_pattern.test(formatString)) 
    var ddd=this.getDayNameAbbr(); 
else if(dd_pattern.test(formatString)) 
    var dd=this.getDateFormatted(); 
else if(d_pattern.test(formatString)) 
    var d=this.getDate();
Community
  • 1
  • 1
Lonely
  • 613
  • 3
  • 6
  • 14
  • Put the print inside the same condition you're setting the variable – Dale Aug 01 '13 at 10:13
  • If you are sure that there will be only one variable assigned, then why don't use just one variable name – Akhil Sekharan Aug 01 '13 at 10:14
  • 2
    why use three variable use one for all – Sonu Sindhu Aug 01 '13 at 10:14
  • `var finalVar = x1 || x2 || x3; console.log(finalVar)` will print the declares var. – mohkhan Aug 01 '13 at 10:14
  • Seriously, stop asking questions if you are not willing to listen to peoples advice! – musefan Aug 01 '13 at 10:15
  • @musefan I can't be able to explain my code better in last question – Lonely Aug 01 '13 at 10:17
  • Why don't you tell us what you are actually trying to achieve overall, because this code just looks like you don't have a clue what you are trying to do – musefan Aug 01 '13 at 10:18
  • What's your code actually doing? Why do you need this kind of logic? – Ja͢ck Aug 01 '13 at 10:18
  • @musefan Yes, I have no clue what I'm trying to do, just got a doubt in the middle, so asked you all guys to help – Lonely Aug 01 '13 at 10:19
  • So what is your requirement?? Why do you even have this code example in the first place? What are you trying to do? What are your conditions? – musefan Aug 01 '13 at 10:20
  • I'm setting a variable if the regex condition is met. and variable should be same as the regex. like var d_pattern=/d{1}/; var dd_pattern=/d{2}/; var ddd_pattern=/d{3}/; var dddd_pattern=/d{4}/; if(dddd_pattern.test(formatString)) var dddd=this.getDayName(); else if(ddd_pattern.test(formatString)) var ddd=this.getDayNameAbbr(); else if(dd_pattern.test(formatString)) var dd=this.getDateFormatted(); else if(d_pattern.test(formatString)) var d=this.getDate(); – Lonely Aug 01 '13 at 10:22
  • Then add that code into the question instead. – Ja͢ck Aug 01 '13 at 10:23
  • @all : sorry, I don't know how to type code in comments – Lonely Aug 01 '13 at 10:23
  • @Jack will do it from now – Lonely Aug 01 '13 at 10:24
  • I have added the code, now explain what you want to do with the final result? Why can't you use one single result variable? like `var result;` – musefan Aug 01 '13 at 10:25
  • the variable name should be same as in regex, then it will make my coding easier – Lonely Aug 01 '13 at 10:26
  • Give me an example of the result you want? And what you plan on doing with that result when you have it? – musefan Aug 01 '13 at 10:28
  • if they give string as D, I show the date as 1-31 if it is DD, then 01-31 if it is DDD, then Sun or Tue etc abbreviated terms if it is DDDD, then Sunday or Tuesday etc.. full name then I will have the variable as D or DD or DDD or DDDD then I directly write the date – Lonely Aug 01 '13 at 10:31
  • OK, so you are writing a date format library? Have you seen [moment.js](http://momentjs.com/)? You can view source code if you want ideas from it. I think you need to reinvent your whole approach an not use so many fixed variables – musefan Aug 01 '13 at 10:34
  • Yes, clearly date format. will work on this approach now, if this doesn't work I've to reinvent as you said. Thanks for the help – Lonely Aug 01 '13 at 10:36
  • What's wrong in my code here http://stackoverflow.com/questions/17989411/way-to-differentiate-between-declared-variable-vs-undeclared/17989639#17989639 – Naveen Kumar Alone Aug 01 '13 at 13:51

4 Answers4

3

Use another variable to hold the information:

var which;
if (condition1) {
    var x1 = 1;
    which = 'x1';
} else if (condition2) {
    var x2 = 2;
    which = 'x2';
} else if (condition3) {
    var x3 = 3;
    which = 'x3';
}
console.log('Declared variable is: ' + which);

But remember, as explained in several answers in your earlier question, hoisting causes all the variables to be declared.

If you want to be able to use which to show the value of the variable, it would be better to use an object, with the name as the property:

var obj = {}, which;
var which;
if (condition1) {
    obj.x1 = 1;
    which = 'x1';
} else if (condition2) {
    obj.x2 = 2;
    which = 'x2';
} else if (condition3) {
    obj.x3 = 3;
    which = 'x3';
}
console.log('Declared variable is: ' + which + ' value is: ' + obj[which]);
Barmar
  • 741,623
  • 53
  • 500
  • 612
2

Why not use an Object?

var obj = {};
if(//conditon){
    obj.x1 = 1;
}
else if(//condition){
    obj.x2 = 2;
}

console.log(Object.keys(obj))

Note: this is the same logic that was given on the last v1 if this question

Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57
0

You can use typeof

if(typeof x1 !== "undefined"){
// ...
}
// other checks
mguimard
  • 1,881
  • 13
  • 14
0

You can iterate the context object by name. In global context its window, so you can iterate window and check
if(window[var_name] === undefined) Regarding your question, there`s one more way to cehck if variable was defined for any context - memoize all variables, then on every change to the context write new list of variables were defined. But it will require to keep list of variables defined for the context you need to track

Yaroslav Yakovlev
  • 6,303
  • 6
  • 39
  • 59