0

my first day in JavaScript and I have a thing I do not understand. I have this piece of code:

var a = [1, 2, 3, 5, 8];
var b = [];
for(var i=0; i<a.length; i++){
    b.push(a[i]);
}
console.log(b);

And the output is:

[ 1, 2, 3, 5, 8 ]
=> undefined

Would you please explain what that undefined means?

6
  • 1
    You're executing this in an interactive console…? Commented Aug 30, 2016 at 14:54
  • 1
    I don't get the undefined part in the Chrome console: jsfiddle.net/8zw3w41t Commented Aug 30, 2016 at 14:54
  • If I run this in a regular page I can't reproduce the undefined. Worked fine. Commented Aug 30, 2016 at 14:54
  • @War10ck I do. Copy and paste everything in the first snippet. Commented Aug 30, 2016 at 14:55
  • @zero298 I did to jsfiddle.net and ran it. I get Array [5] Commented Aug 30, 2016 at 14:56

5 Answers 5

5

You are calling a function console.log and it the function does not retun any value, then it is undefined.

So in this case

console.log displays array b and at the end it displays the value returned by the function. which is in this case nothing. So undefined will be displayed.

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

Comments

4

You are likely running this in the browser's console. It is printing the returned value after calling console.log().

Console output

console.log() returns undefined. What undefined is is a whole other question. Suffice it to say, with respect to this instance, it is the default value returned by a function that has no explicit return statement.


You asked:

so basically every time i call a function that returns void (in this case console.log()) i will get this evaluation, right? undefined.

Don't confuse void and undefined. They are not the same. You can't even return void, it's a keyword for an operator: void operator. If you try to return it, you'll get a syntax error.

If a function that you define does not provide an explicit return, it will return undefined by default:

function foo(){
    var a = 2 + 2;
}
var bar = foo(); // bar has the value "undefined"

undefined is rather ironic, because it is a special, defined value. You can do comparisons with it, you can assign things to it, you can return it explicitly. It is a special falsy value.

1 Comment

so basically every time i call a function that returns void (in this case console.log()) i will get this evaluation, right? undefined.
2

You get an undefined because nothing is being returned from the JavaScript you are writing.

Even if you just write this in the console:

console.log('Bar');

You would see undefined;

However, if you ran this in the console

function returnSomething() {
    return 'Hello';
}
returnSomething();

You would see "Hello", and no longer see undefined.

Comments

0

In this instance, "undefined" just means that the script has finished running. It looks like your program does what it is supposed to do, it populates 'b' and the logs it to the console. When that is done, the script finishes itself and outputs "undefined" because it's done.

1 Comment

Not because "it's done", but because the last expression returned undefined.
0

When you execute things in the console, the result of every line you execute will be logged.

For example, if you run var x = 5; in the console, you will also see undefined. That is because there if no output for var x = 5;

After running that, if you run x = 9; in the console, you will see 9 after that line. This means that the output of x = 9 is 9 (this is just a something that the creators of JavaScript decided, but useful to know).

Just like creating a variable does not output anything, if you call a function that does not return anything, you will see undefined printed as well.

console.log is a function that doesn't return anything, but it is a function that adds text to the console. So the first thing that you are seeing is the text that console.log(b) is outputting (your array) and the second line is the output of the function (undefined).

Hope this helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.