0

When I run a function to get all available variables from the window… it will only show non declared variables properly

Once variables have been declared:

  1. var
  2. let
  3. const

Code breaks…

Whats going on?
Can this be fixed?

Heads up
I don't want to use iframe…

What works

a = 'apple';
b = 'banana';
c = 'cherry';
d = /dog/gi;
e = 'elephant';
f = 'fish';
g = 'ground';
h = 'horse';

windowCounter = 0;

function Variables() {
  for (let i in window) {
    if (window.hasOwnProperty(i)) {
      windowCounter++;
      if (windowCounter > 165) {
        console.log(i+' = '+window[i]);
      }
    }
  };
}
Variables();



Problem

let a = 'apple';
const b = 'banana';
const c = 'cherry';
let d = /dog/gi;
var e = 'elephant';
f = 'fish';//This is the only one that works properly
let g = 'ground';
var h = 'horse';

windowCounter = 0;//And this one…

function Variables() {
  for (let i in window) {
    if (window.hasOwnProperty(i)) {
      windowCounter++;
      if (windowCounter > 165) {
        console.log(i+' = '+window[i]);
      }
    }
  };
}
Variables();
Allen Marshall
  • 381
  • 2
  • 10
  • Have you tested this anywhere outside of an SO Code Snippet/JSFiddle/whatever other artificial testing environment? Mostly the behaviour lies there. (Apart from `let` and `const`, where this behaviour is expected.) – deceze Oct 27 '17 at 15:03
  • Yes locally…` var` works locally… `let and const ` doesn't though… that's where the true problem lies. I was wonder if there was another way of getting them or can the code me modified the get `let and const` – Allen Marshall Oct 27 '17 at 15:06

0 Answers0