randy randy - 5 months ago 8
JSON Question

Javascript console.log working with variable

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?

Answer

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.