4

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.

10
  • When I do console.log(x), I get 123 (x), then undefined (since console.log() doesn't return anything) Commented Jul 11, 2018 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 Commented Jul 11, 2018 at 21:39
  • Yes, as @dgeare stated. Why don't I see the actual value of x gets printed using console.log? Commented Jul 11, 2018 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? Commented Jul 11, 2018 at 21:41
  • 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. lifewire.com/about-blank-4125143 Commented Jul 11, 2018 at 21:52

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I did exactly as you did but I still see undefined although the message count is increasing after each print.
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

5 Comments

Even though it's redundant it should still print something. This doesn't explain why it's not.
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
The question isn't about the value, it's about why he's not seeing the output of it.
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.
I need console.log to print because I can't log inside a function as you suggest.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.