3

Why the result of printing x using console.log is undefined?

enter image description here


Update:

This is not a duplicate. I'm trying to understand why console.log doesn't print.

I'm not talking about the return value of console.log.

Stav Alfi
  • 13,139
  • 23
  • 99
  • 171
  • When I do `console.log(x)`, I get `123` (x), then `undefined` (since console.log() doesn't return anything) –  Jul 11 '18 at 21:34
  • 1
    @dAxx_ i don't think it's a dup. The undefined from console.log doesn't explain why the value of x wasn't logged. The expected output for `console.log(x)` would have been `123` (newline) `undefined` – dgeare Jul 11 '18 at 21:39
  • Yes, as @dgeare stated. Why don't I see the actual value of `x` gets printed using `console.log`? – Stav Alfi Jul 11 '18 at 21:40
  • @StavAlfi maybe x is being modified on the page? do you get the same behavior from a more distinctly unique variable name? – dgeare Jul 11 '18 at 21:41
  • @dgeare Yes, I do. – Stav Alfi Jul 11 '18 at 21:43
  • I have a theory... what's the output of `console.log(console.log)`? it should be `ƒ log() { [native code] }`. But if not... it's possible something is overwriting it. I can emulate this same output if, before everything else, I write `console.log = () => {}` – dgeare Jul 11 '18 at 21:45
  • the output of `console.log(console.log)` is `undefined` – Stav Alfi Jul 11 '18 at 21:46
  • that's the problem I think. something is overwriting it. what's the behavior in an incognito `about:blank` – dgeare Jul 11 '18 at 21:48
  • `about:blan` -> `Uncaught ReferenceError: blank is not defined at :1:7` – Stav Alfi Jul 11 '18 at 21:50
  • 1
    sorry, to clarify about:blank is the page/url you open up in chrome. Just an HTML doc with nothing on it. I just wanted to remove all extraneous environment variables/scripts. https://www.lifewire.com/about-blank-4125143 – dgeare Jul 11 '18 at 21:52

3 Answers3

2

So I dig a little bit and I found something that could of happen to you.
when you open your Dev Tools, you have this little icon on the left: enter image description here

if you clicked on it, it will open a sidebar and if you are marking one of the two options that I point, it will not show the console.log print.
Change to the first selection, and you'll find out that you get the print. enter image description here

dAxx_
  • 2,210
  • 1
  • 18
  • 23
  • I did exactly as you did but I still see `undefined` although the message count is increasing after each print. – Stav Alfi Jul 11 '18 at 22:02
0

Logging to the console is a redundant action in devtools, since it is bound to output to the console any values that are passed to it.

x

Is the proper way to print the value of x to the console.

console.log(x)

Is basically the same as writing

console.log(console.log(x))

Which returns undefined. Though, if you have an active debugger, it will evaluate and also print the value

Erik
  • 108
  • 2
  • 10
  • 1
    Even though it's redundant it should still print something. This doesn't explain why it's not. – Barmar Jul 11 '18 at 21:59
  • because console.log(x) has no value. Running console.log() does not return a value. If you had a function: function returnValue(value){ return value } then ran: console.log(returnValue(x)), it would log the value returned by the function, but console.log doesn't do that, it prints to the console, which you are already doing by typing into the console, so logging the returned value of console.log() returns undefined – Erik Jul 11 '18 at 22:11
  • The question isn't about the value, it's about why he's not seeing the output of it. – Barmar Jul 11 '18 at 22:14
  • He types `console.log(x)`, so it should display the value of `x` and then return `undefined`. But all he sees is the returned value. – Barmar Jul 11 '18 at 22:14
  • I need `console.log` to print because I can't log inside a function as you suggest. – Stav Alfi Jul 11 '18 at 22:33
0

I found an answer here: Chrome: console.log, console.debug are not working:

By @Tim:

Same issue, but I just cleared my settings. I went into Settings > Preferences and Clicked [Restore defaults and reload]. Just remember what your settings were.

enter image description here

Stav Alfi
  • 13,139
  • 23
  • 99
  • 171