Removing undefined from javascript output [duplicate] - javascript

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.

Related

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 does this `do`–`while` loop repeat the last value after the end? [duplicate]

This question already has answers here:
Javascript while loop return value
(3 answers)
In Javascript While loop repeats last number when counting from 1 to 5 when run on console [duplicate]
(2 answers)
Closed 4 years ago.
I tried the following code in the Google Chrome console and I’m getting this output. Why is there an additional 4 printed?
var i = 0;
do{
console.log(i);
i++;
} while(i < 5);
Output:
0
1
2
3
4
4
There is no extra 4 at end. Your loop is correct and works just fine :). That's return value of the i++ expression, which you are mistaking for console.log in developer console. Try this, you will figure it out;
var i=0;do{console.log('i = ', i);i++;}while(i<5);
Edit Thanks to #ggorlen for pointing this out, it's the return value of last expression in block({}) which is i++, i increments the value and returns it's last value (if i = 4; i++ returns 4 and makes value of i = 5), (++i, returns 5 and makes value of i = 5)
var i=0;do{console.log('i = ', i);++i;}while(i<5);
will give return value of 5
JavaScript has the concept of "Completion Records". Basically every statement produces a completion record.
The Completion type is a Record used to explain the runtime propagation of values and control flow such as the behaviour of statements (break, continue, return and throw) that perform nonlocal transfers of control.
You usually cannot do anything with these in user land code, but the runtime needs those to properly execute your program. However the Chrome console will print the value of the completion record of the last statement it executes.
Without going into to much details, here is what happens:
The value of the completion record of the do...while loop is the value of the completion record of its body.
The body is a block with a statement list. The value of the completion value of a statement list is the value of the completion record of the last statement in the list.
The last statement is an expression statement (i++;). The value of the completion record of an expression is the value the expression evaluates to.
The simplest example to demonstrate this behavior is simply
42;
The result of evaluating this expression statement is a completion record that looks something like
{
type: normal,
value: 42,
target: empty
}
You will see it print 42 in the console because that's the value of the completion record of the expression statement.
A slightly more evolved example:
if (true) {
42;
}
will print the same.
Yeah, it's normal that extra 4.
It's called expression evaluation. Id doesn't mean it's printing, but instead evaluating i to you.
Check here to know more about it.
This is just chrome helping you to know the dynamic environment of your variables.

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.

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

javascript variable's weird behaviour [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 6 years ago.
Case 1 - If I console.log(variable) before the variable declaration I get undefined. eg;
// code
console.log(a);
var a ;
// output
undefined
Case 2 - If I console.log(variable) without variable declaration I get Uncaught ReferenceError: variable is not defined.
// code
console.log(a);
// output
Uncaught ReferenceError: a is not defined
But incase of functions we can call a function before or after function definition it never give any issue. eg;
console.log(example());
function example(){
return 'test done';
}
console.log(example());
// output without any issue
Now My question is, what is the difference between undefined and not defined.
Undefined means - variable exists, but have no any stored value in it.
Not defined means - variable not declared (not exist).

Categories