Joe Consterdine Joe Consterdine - 28 days ago 8
Javascript Question

Is this a shorthand for console.log?

In advanced Javascript by Kyle Simpson he says that this:

var foo = "bar";


can be evaluated like this:

foo;


And that it's just a shortcut for console.log.

But when I try it nothing shows in the console.

Why is that?

Cheers

Answer

If you type that in the console and press Enter, the console will show you the result of the expression, which is the value of foo. The console shows you the resulting value of any expression you type into it. In the console itself, there's rarely any need to type console.log. (In fact, if you typed console.log(foo); into the console and pressed Enter, you'd see the value of foo followed by undefined, because console.log returns undefined, so the console shows you that value.)

In code not typed into the console itself, no, foo; is not shorthand for console.log(foo);. It's just an ExpressionStatement sitting on its own that basically does nothing.