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
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.
foo;
is not a shorthand for console.log(foo)
.
Suppose in code, you have defined a variable var name="mrid"
you'll have to write console.log(name)
to print it in console. But you can directly type name
in console and it will print it's value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.