-4

How to get all undeclared variables from java script. I need list of all the undeclared variables. I want the list of undefined javascript variables in java code. Right now I am using Rhino engine to evaluate javascript. Any idea?

satu
  • 19
  • 2
  • 1
    Define "undeclared variables". – Šime Vidas Apr 03 '13 at 12:51
  • it is not important to declare variables in javascript. It is not an error – AurA Apr 03 '13 at 12:52
  • 1
    @AurA So you're writing in the default language? Well, switch to the strict language, you're gonna be left behind. – Šime Vidas Apr 03 '13 at 12:53
  • @ŠimeVidas I do program in strict language like Java, and also I would not recommended to directly use variables, there is a difference in Javascript too... http://stackoverflow.com/questions/1470488/difference-between-using-var-and-not-using-var-in-javascript – AurA Apr 03 '13 at 13:01
  • @AurA No, you misunderstood. There exist two JavaScript languages: strict JavaScript, and default JavaScript. In strict JavaScript one must declare variables. – Šime Vidas Apr 03 '13 at 13:06
  • @ŠimeVidas You may be right, I had no idea of strict javascript. – AurA Apr 03 '13 at 13:07

2 Answers2

4

If you run your code through JSLint you will get warnings for undeclared variables among other warnings. For example running the following:

"use strict";

function b() {
    a = 20;
}

Will give this as one of its warnings

'a' was used before it was defined.      line 4 character 5

     a = 20;

I included "use strict"; in the above so the warnings come out, if you include it in your code normally use of undefined variables will throw exceptions which you can view in the browser's developer tools.

Community
  • 1
  • 1
Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
0

if you mean undeclared variables value is undefined, the following code will work only for global window scope

var a ;
var undefinedVariables = [];

for ( var i in window ){
    if ( window[i] === undefined ){
        undefinedVariables.push( i )
    }
}

console.log( undefinedVariables );

which print ["a"]

rab
  • 4,134
  • 1
  • 29
  • 42
  • Thanks for your immediate reply but i want the list of undefined javascript variables in java code. Right now I am using Rhino engine to evaluate javascript. – satu Apr 03 '13 at 13:03