0

Inside the console, you can simply declare variables and use them.

var x = 'thing';

undefined

x + 3;

'thing3'

I want set that variable from a Javascript file, and use it in the console. That does not seem possible, so I hope somebody knows a solution for it.

In a javascript file, typing console.log(var x = 'thing') gives compilation errors in Gulp. I know I can just type console.log(x), but I want to use the variable and play with it in the console.

How can I declare variables that I can use in the console?

Randy
  • 9,419
  • 5
  • 39
  • 56

3 Answers3

1

You are looking for a simple

var x = {thing: 'thing'};
console.log(x);

The undefined in the console comes from evaluating a statement that doesn't return anything. If you don't evaluate it in the console, but put it into your js files, there won't be any log.

I'm trying to debug my webpage using console.log

Don't. Use a debugger. You can statically insert breakpoints in your code with a debugger; statement.

How can I interact with logged variables from a JS file in the console?

The best thing of breakpoints in a debugger is: you can interact with any variables in the scope of the breakpoint without even needing to log them.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I've never used the debugger, but obviously thats just my bad. Thx for the info, checking if that's what I need! – Randy Jun 17 '16 at 09:48
  • This is the answer. I can put a `debugger;` statement in my class and call it's variables as if they were global, except I have to call them with `this.varname` instead of `varname`. I have found a second solution; if I set my variable in the `window` object, they are global and usable in the console as well. Thank you for the info! – Randy Jun 17 '16 at 09:57
0

Maybe use console.debug(myTest.test)

Rene Vorndran
  • 693
  • 1
  • 6
  • 13
0

I will suggest you to use the debugger rather than using global variables as it's not secure if you are able to access those variables in console. Use chrome while debugging. Open console and select the js file which you want to debug and put breakpoints.

Sheshaj Awasthi
  • 137
  • 1
  • 4