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

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.

Related

Logical operators Javascript && [duplicate]

This question already has answers here:
Does JavaScript have "Short-circuit" evaluation?
(3 answers)
Closed 2 years ago.
When I run this code firstly shows me 1 and then undefined. But I still couldn't understand it.
alert(alert(1) && alert(2));
Here is some explanation :
The call to alert returns undefined (it just shows a message, so there's no meaningful return).
Because of that, && evaluates the left operand (outputs 1), and immediately stops, because undefined is a falsy value. And && looks for a falsy value and returns it, so it's done.
alert does not return anything
So alert(1) runs, since alert does not return anything it is undefined. As a result you have alert(undefined && alert(2)). The alert(2) will not execute because left part needs to be truthy to execute. Undefined is falsey. That evaluates to alert(undefined).
So you get alert of 1 for the first alert and you get undefined for the outside alert and the alert(2) is never executed.
Code written out in a less confusing manner
var action = alert(1) && alert(2) // only runs alert(1)
console.log(action) // shows variable is undefined
alert(action) // alerts undefined

console.log prints undefined [duplicate]

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

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