How to console.log() while in Chrome console? [duplicate] - javascript

Every time console.log is executed, a line saying undefined is appended to the output log.
It happens in both Firefox and Chrome on Windows and Linux.

If you're running console.log() from a JS file, this undefined line should not be appended.
If you're running console.log() from the console itself, it makes sense. This is why: In the console you can type a name of a variable (for example try typing window) and it prints info about it. When you run any void function (like console.log) from the console, it also prints out info about the return value, undefined in this case.
I tested both cases on my Chrome (Mac ver 23.0.1271.101) and indeed I see the undefined line when I run it inside the console. This undefined also appears when I write this line in the console: var bla = "sdfdfs"

Although talkol´s answer is ok, I try to put it more straight:
JavaScript is designed as a dynamic language which means that the type (string, void, boolean …) of a function return value is not pre-defined. If a function does not use a return statement or an empty return statement with no value, JavaScript automatically returns undefined. That means that in JavaScript every function returns something, at least undefined.
So the function console.log() in Chrome console either uses no or an empty return statement, so that the return value of this function is undefined. This function return value gets also displayed in the Chrome console.
[If somebody know where to find the definition of the console.log() function in Google Chrome source code, please comment with the link, then we can even go further and look at the real code, would be nice.]
Sources:
https://stackoverflow.com/a/20915524/1744768
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

Follow the picture to solve this problem:
Ctrl + Shift + J

Console environment in your browser is designed to take the very last statement expression in a program and evaluate it for a value and then show you that value.
The result of an assignment expression is the value that was assigned.
So the JavaScript engine just does an assignment but the console does one extra step which is to set whatever my last statement is, give you that value back. That’s why it prints 2:
In statements that have no return value you get something like undefined.

undefined is the return value of the console.log() in Chrome developer tools. You will get undefined if you do the following in Chrome developer tools, and you will see that you get undefined even though x has the value 3.
> let x = 3
> undefined

What you can do is simply create your own console.log like function with a return to change this behavior when doing a lot of coding in the developer console. Here is an example of what that looks like in the developer console:
console.log('I hate seeing the next line stating the obvious.')
I hate seeing the next line stating the obvious.
undefined
log = function(l){return l}
function log()
if(1 === 2){console.log('1 is not equal to 2.')}else{log('No Shit Sherlock.')}
"No Shit Sherlock."

That undefined you see in console is the return value of the function:
check out these two variants:
This one returns nothing
This one returns something:

Remember one thing. Any function that has some definition will always return something, If you skip the return keyword, it will eventually return undefined when you call it.

If you're using console.log to emit multiple values in a single line, here's a hacky alternative:
var1 + ' ' + var2 + ' ' + var...
(Better ideas welcome, this might blow up in certain circumstances)

Related

Why inner function (closure) contain undefined value when displayed in console [duplicate]

Every time console.log is executed, a line saying undefined is appended to the output log.
It happens in both Firefox and Chrome on Windows and Linux.
If you're running console.log() from a JS file, this undefined line should not be appended.
If you're running console.log() from the console itself, it makes sense. This is why: In the console you can type a name of a variable (for example try typing window) and it prints info about it. When you run any void function (like console.log) from the console, it also prints out info about the return value, undefined in this case.
I tested both cases on my Chrome (Mac ver 23.0.1271.101) and indeed I see the undefined line when I run it inside the console. This undefined also appears when I write this line in the console: var bla = "sdfdfs"
Although talkol´s answer is ok, I try to put it more straight:
JavaScript is designed as a dynamic language which means that the type (string, void, boolean …) of a function return value is not pre-defined. If a function does not use a return statement or an empty return statement with no value, JavaScript automatically returns undefined. That means that in JavaScript every function returns something, at least undefined.
So the function console.log() in Chrome console either uses no or an empty return statement, so that the return value of this function is undefined. This function return value gets also displayed in the Chrome console.
[If somebody know where to find the definition of the console.log() function in Google Chrome source code, please comment with the link, then we can even go further and look at the real code, would be nice.]
Sources:
https://stackoverflow.com/a/20915524/1744768
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
Follow the picture to solve this problem:
Ctrl + Shift + J
Console environment in your browser is designed to take the very last statement expression in a program and evaluate it for a value and then show you that value.
The result of an assignment expression is the value that was assigned.
So the JavaScript engine just does an assignment but the console does one extra step which is to set whatever my last statement is, give you that value back. That’s why it prints 2:
In statements that have no return value you get something like undefined.
undefined is the return value of the console.log() in Chrome developer tools. You will get undefined if you do the following in Chrome developer tools, and you will see that you get undefined even though x has the value 3.
> let x = 3
> undefined
What you can do is simply create your own console.log like function with a return to change this behavior when doing a lot of coding in the developer console. Here is an example of what that looks like in the developer console:
console.log('I hate seeing the next line stating the obvious.')
I hate seeing the next line stating the obvious.
undefined
log = function(l){return l}
function log()
if(1 === 2){console.log('1 is not equal to 2.')}else{log('No Shit Sherlock.')}
"No Shit Sherlock."
That undefined you see in console is the return value of the function:
check out these two variants:
This one returns nothing
This one returns something:
Remember one thing. Any function that has some definition will always return something, If you skip the return keyword, it will eventually return undefined when you call it.
If you're using console.log to emit multiple values in a single line, here's a hacky alternative:
var1 + ' ' + var2 + ' ' + var...
(Better ideas welcome, this might blow up in certain circumstances)

When I use template literals and run a code, I get undefined in result [duplicate]

Every time console.log is executed, a line saying undefined is appended to the output log.
It happens in both Firefox and Chrome on Windows and Linux.
If you're running console.log() from a JS file, this undefined line should not be appended.
If you're running console.log() from the console itself, it makes sense. This is why: In the console you can type a name of a variable (for example try typing window) and it prints info about it. When you run any void function (like console.log) from the console, it also prints out info about the return value, undefined in this case.
I tested both cases on my Chrome (Mac ver 23.0.1271.101) and indeed I see the undefined line when I run it inside the console. This undefined also appears when I write this line in the console: var bla = "sdfdfs"
Although talkol´s answer is ok, I try to put it more straight:
JavaScript is designed as a dynamic language which means that the type (string, void, boolean …) of a function return value is not pre-defined. If a function does not use a return statement or an empty return statement with no value, JavaScript automatically returns undefined. That means that in JavaScript every function returns something, at least undefined.
So the function console.log() in Chrome console either uses no or an empty return statement, so that the return value of this function is undefined. This function return value gets also displayed in the Chrome console.
[If somebody know where to find the definition of the console.log() function in Google Chrome source code, please comment with the link, then we can even go further and look at the real code, would be nice.]
Sources:
https://stackoverflow.com/a/20915524/1744768
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
Follow the picture to solve this problem:
Ctrl + Shift + J
Console environment in your browser is designed to take the very last statement expression in a program and evaluate it for a value and then show you that value.
The result of an assignment expression is the value that was assigned.
So the JavaScript engine just does an assignment but the console does one extra step which is to set whatever my last statement is, give you that value back. That’s why it prints 2:
In statements that have no return value you get something like undefined.
undefined is the return value of the console.log() in Chrome developer tools. You will get undefined if you do the following in Chrome developer tools, and you will see that you get undefined even though x has the value 3.
> let x = 3
> undefined
What you can do is simply create your own console.log like function with a return to change this behavior when doing a lot of coding in the developer console. Here is an example of what that looks like in the developer console:
console.log('I hate seeing the next line stating the obvious.')
I hate seeing the next line stating the obvious.
undefined
log = function(l){return l}
function log()
if(1 === 2){console.log('1 is not equal to 2.')}else{log('No Shit Sherlock.')}
"No Shit Sherlock."
That undefined you see in console is the return value of the function:
check out these two variants:
This one returns nothing
This one returns something:
Remember one thing. Any function that has some definition will always return something, If you skip the return keyword, it will eventually return undefined when you call it.
If you're using console.log to emit multiple values in a single line, here's a hacky alternative:
var1 + ' ' + var2 + ' ' + var...
(Better ideas welcome, this might blow up in certain circumstances)

Does Function Calls Depend on it's Placement in the Code Structure [duplicate]

Every time console.log is executed, a line saying undefined is appended to the output log.
It happens in both Firefox and Chrome on Windows and Linux.
If you're running console.log() from a JS file, this undefined line should not be appended.
If you're running console.log() from the console itself, it makes sense. This is why: In the console you can type a name of a variable (for example try typing window) and it prints info about it. When you run any void function (like console.log) from the console, it also prints out info about the return value, undefined in this case.
I tested both cases on my Chrome (Mac ver 23.0.1271.101) and indeed I see the undefined line when I run it inside the console. This undefined also appears when I write this line in the console: var bla = "sdfdfs"
Although talkol´s answer is ok, I try to put it more straight:
JavaScript is designed as a dynamic language which means that the type (string, void, boolean …) of a function return value is not pre-defined. If a function does not use a return statement or an empty return statement with no value, JavaScript automatically returns undefined. That means that in JavaScript every function returns something, at least undefined.
So the function console.log() in Chrome console either uses no or an empty return statement, so that the return value of this function is undefined. This function return value gets also displayed in the Chrome console.
[If somebody know where to find the definition of the console.log() function in Google Chrome source code, please comment with the link, then we can even go further and look at the real code, would be nice.]
Sources:
https://stackoverflow.com/a/20915524/1744768
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
Follow the picture to solve this problem:
Ctrl + Shift + J
Console environment in your browser is designed to take the very last statement expression in a program and evaluate it for a value and then show you that value.
The result of an assignment expression is the value that was assigned.
So the JavaScript engine just does an assignment but the console does one extra step which is to set whatever my last statement is, give you that value back. That’s why it prints 2:
In statements that have no return value you get something like undefined.
undefined is the return value of the console.log() in Chrome developer tools. You will get undefined if you do the following in Chrome developer tools, and you will see that you get undefined even though x has the value 3.
> let x = 3
> undefined
What you can do is simply create your own console.log like function with a return to change this behavior when doing a lot of coding in the developer console. Here is an example of what that looks like in the developer console:
console.log('I hate seeing the next line stating the obvious.')
I hate seeing the next line stating the obvious.
undefined
log = function(l){return l}
function log()
if(1 === 2){console.log('1 is not equal to 2.')}else{log('No Shit Sherlock.')}
"No Shit Sherlock."
That undefined you see in console is the return value of the function:
check out these two variants:
This one returns nothing
This one returns something:
Remember one thing. Any function that has some definition will always return something, If you skip the return keyword, it will eventually return undefined when you call it.
If you're using console.log to emit multiple values in a single line, here's a hacky alternative:
var1 + ' ' + var2 + ' ' + var...
(Better ideas welcome, this might blow up in certain circumstances)

undefined returned when adding a new property to an object [duplicate]

Every time console.log is executed, a line saying undefined is appended to the output log.
It happens in both Firefox and Chrome on Windows and Linux.
If you're running console.log() from a JS file, this undefined line should not be appended.
If you're running console.log() from the console itself, it makes sense. This is why: In the console you can type a name of a variable (for example try typing window) and it prints info about it. When you run any void function (like console.log) from the console, it also prints out info about the return value, undefined in this case.
I tested both cases on my Chrome (Mac ver 23.0.1271.101) and indeed I see the undefined line when I run it inside the console. This undefined also appears when I write this line in the console: var bla = "sdfdfs"
Although talkol´s answer is ok, I try to put it more straight:
JavaScript is designed as a dynamic language which means that the type (string, void, boolean …) of a function return value is not pre-defined. If a function does not use a return statement or an empty return statement with no value, JavaScript automatically returns undefined. That means that in JavaScript every function returns something, at least undefined.
So the function console.log() in Chrome console either uses no or an empty return statement, so that the return value of this function is undefined. This function return value gets also displayed in the Chrome console.
[If somebody know where to find the definition of the console.log() function in Google Chrome source code, please comment with the link, then we can even go further and look at the real code, would be nice.]
Sources:
https://stackoverflow.com/a/20915524/1744768
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
Follow the picture to solve this problem:
Ctrl + Shift + J
Console environment in your browser is designed to take the very last statement expression in a program and evaluate it for a value and then show you that value.
The result of an assignment expression is the value that was assigned.
So the JavaScript engine just does an assignment but the console does one extra step which is to set whatever my last statement is, give you that value back. That’s why it prints 2:
In statements that have no return value you get something like undefined.
undefined is the return value of the console.log() in Chrome developer tools. You will get undefined if you do the following in Chrome developer tools, and you will see that you get undefined even though x has the value 3.
> let x = 3
> undefined
What you can do is simply create your own console.log like function with a return to change this behavior when doing a lot of coding in the developer console. Here is an example of what that looks like in the developer console:
console.log('I hate seeing the next line stating the obvious.')
I hate seeing the next line stating the obvious.
undefined
log = function(l){return l}
function log()
if(1 === 2){console.log('1 is not equal to 2.')}else{log('No Shit Sherlock.')}
"No Shit Sherlock."
That undefined you see in console is the return value of the function:
check out these two variants:
This one returns nothing
This one returns something:
Remember one thing. Any function that has some definition will always return something, If you skip the return keyword, it will eventually return undefined when you call it.
If you're using console.log to emit multiple values in a single line, here's a hacky alternative:
var1 + ' ' + var2 + ' ' + var...
(Better ideas welcome, this might blow up in certain circumstances)

node.js displays "undefined" on the console

Recently I installed node.js on my Windows 7 machine.
On execution of JavaScript, I get an undefined message along with successful execution of the expression.
What's wrong here? I have not noticed any other side effects.
The JavaScript functions always return something. If you don't specify something to return in the function, 'undefined' is returned by default (you can check this out in Firebug too).
Don't worry though, this doesn't affect anything, you can ignore it.
Just write "hello world"; and hit enter... it will return "hello world" instead of undefined, thus no undefined is displayed. console.log returns undefined and also logs arguments to console so you get multiple messages.
As pointed out by others, javascript function will always return undefined if you do not specify any return value. You can just ignore it. It's not going to cause any harm.
But if it's annoying you too much then you can turn it off in repl. Repl has this property ignoreUndefined which is set to false by default. You can set it to true.
Try this:
module.exports.repl.ignoreUndefined = true;
Well, the question was made some years ago, but there is still another way to explain what is happening here.
Try next command:
console.log("Hello World") || "Bye World";
As mentioned function console.log() returns undefined normally and you can choose a better return value. Since Non-strict (abstract) comparison considers undefined equal to false the || operator allows you that choice.
Considering that it is just a debugging tool, this is unnecessary in general use, but helps to understand that console displays text sent to stdout and also evals the command and displays the returned value, or undefined if no value was received.
If you need to turn on ignoreUndefined from inside a module, you can use the following line to simulate AvinashB's answer without typing into the console by yourself:
process.stdin.emit('data', "module.exports.repl.ignoreUndefined = true;\n");
node also prints undefined because IN GENERAL, it displays the return value of each command and console.log doesn't return anything.
this is happening because console.log has (null/undefined) return type
to solve this issue.. have a function returning a string..
then log that function returned value...
that will overcome the undefined issue..
const addNotes= function(msg)
{
return msg;
}
console.log(addNotes('say hello'));
Just ignore it, no need to worry at all. I too had the same issue when I called the function without any return type. So node used to print undefined return type for the called function.
function emitEvent(eventname){
eventEmitter.emit(SIV_EVENT);
}
console.log(emitEvent());
Check undefined here, https://nodejs.org/api/util.html.
To avoid seeing undefined output from commands when using Node's repl module, set ignoreUndefined to true in repl.start.

Categories