console.log prints undefined [duplicate] - javascript

This question already has answers here:
Chrome: console.log, console.debug are not working
(23 answers)
Closed 1 year ago.
When I try to print a simple string with console.log(), it does not print the string out but rather it returns undefined:
> console.log("Hello")
< undefined
What is wrong?

Check your console log levels configuration. Chances are you unchecked info :

because when you write in console it will print the return value of the function, when the function you called have no explicit return javascript implicitly call return undefined.
so when you write console.log("hello") it will return undefined so console print undefined for you

just place your javascript.min.js file within the body tag of html and test

Related

Print undefined in console with no reason, Javascript [duplicate]

This question already has answers here:
Chrome/Firefox console.log always appends a line saying 'undefined'
(9 answers)
Closed 5 years ago.
I have the below block of code which in the console print "undefine" twice after "Hello"
JavaScript Code
function f()
{
function g() {
console.log("Hello");
}
return g;
}
var x = f()();
console.log(x);
While I am trying to print only "Hello", where is the two undefined printing from
Output in console
Hello
undefined
undefined
The first undefined is because f()() evaluates to whatever g returns, and g doesn't return anything. You store that undefined in x, then print it when you write console.log(x);.
I'm guessing the second undefined is the result of running this in a console. The last line containing console.log evaluates to undefined, which may be implicitly printed from the console/REPL. You can confirm this by adding something like the string "World" as the last line of the script. Adding anything else to the last line should get rid of the second undefined.

logging javascript object properties [duplicate]

This question already has answers here:
console.log() async or sync?
(3 answers)
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Closed 5 years ago.
Please share your thoughts regarding below outputs.
var a = '';
console.log(a); //this logs nothing as expected.
a = 'triven';
var a = {};
console.log(a); // How javascript is aware of property here that is defined below.
a.name = 'triven';
I think you're looking for console.dir().
console.log() doesn't do what you want because it prints a reference to the object, and by the time you pop it open, it's changed. console.dir prints a directory of the properties in the object at the time you call it.
The JSON idea below is a good one; you could even go on to parse the JSON string and get a browsable object like what .dir() would give you:
console.log(JSON.parse(JSON.stringify(obj)));

Why this piece of code prints `undefined` at the end? [duplicate]

This question already has answers here:
What does it mean if console.log(4) outputs undefined in Chrome Console?
(2 answers)
Closed 5 years ago.
While I am running the following piece of code in Chrome's developer tools javascript console,
var solution = 1;
for(var i = 1; i <= 12; ++i)
solution *= i;
console.log(solution.toString());
I get the following output:
479001600
undefined
When I remove the last line, that is console.log(solution.toString());, I get just 479001600. What happens?
The undefined is simply the console's way of telling you that the statement:
console.log(solution.toString());
doesn't, itself, return a value (it outputs a value, but doesn't return one). It's not something to worry about when your actual code executes because it's a specific behavior to the developer's console.
As another example, if you type: 5 + 6 into the console, it will report 11 on the next console line, because the console always wants to report any value returned from the statement it just executed.
The console in google chrome will always return the value of the last executed statement.
Since your last statement in the console was
console.log()
one which did not return any value it showed 'undefined';
Try
var a=[];a.push(5);
It will show 1 on the next line as the push method returns the length of the newly formed array

Printing object property also prints undefined? [duplicate]

This question already has answers here:
Chrome/Firefox console.log always appends a line saying 'undefined'
(9 answers)
Closed 7 years ago.
I have a JavaScript object:
function Thing() {
this.number = 4;
}
I create an instance and assign a new property:
var myThing = new Thing();
myThing.newProperty = 5;
console.log(myThing.newProperty);
and the output is:
5
undefined
Why is the output also printing undefined?
You don't need to type console.log() into the console. If you type in a variable, it'll print its value.
When you do console.log(myThing.newProperty); in the console, it runs it and shows you its return value.
5 is shown because you ran console.log. undefined is shown because that's console.log's return value.

Removing undefined from javascript output [duplicate]

This question already has an answer here:
Console returns undefined [duplicate]
(1 answer)
Closed 7 years ago.
Running the following script in javascript or nodejs or any other javascript environment prints:
undefined
0
1
2
3
4
Script:
for(var i=0;i<5;i++){
var a = function (i) {
setTimeout(function () {
console.log(i);
},i*1000);
};
a(i);
}
Where does the undefined comes from?
When using a REPL environment, the expression you enter is evaluated and its result is returned.
In this case, the result is undefined. It's a side effect of the REPL, it's not part of the output of your code.
If you’re running it from the REPL, undefined is the completion value of the expression, which is a(4). a doesn’t return anything, so its return value is undefined, and the REPL prints that. It is not passed to console.log and won’t appear if you run it as a standalone script.

Categories