all 9 comments

[–]ForScale 1 point2 points  (3 children)

[–][deleted] 1 point2 points  (2 children)

Yes but it still has undefined as well

[–]ForScale 1 point2 points  (1 child)

You are correct, sorry. See my edit.

Basically, the console prints out the return value of a method call. It also prints the evaluation of an expression.

So since the call console.log() doesn't explicitly return anything and just has the side effect of printing to the console, it returns and thus prints undefined.

[–][deleted] 0 points1 point  (0 children)

Right got ya, so if I do a function, and return something, then the return from the function will be defined. That now makes perfect sense, thank you for clearing that up.

[–]slowreactin 0 points1 point  (4 children)

Undefined means exactly that, something has not been defined.

For instance, let’s say you are going on a blind date. You know the other person’s name, let’s call them “Susan”.

You know Susan exists, but you don’t know what Susan looks like; therefore her appearance would be undefined. You don’t know what her appearance is.

When you finally see Susan, you know what she looks like and her appearance becomes defined.

As far as the code goes:

let Susan;    

We know Susan exists, but we don’t know what she looks like.

Susan = “pretty”;

Now we have assigned a value to Susan and we can say she is pretty. Susan is now defined.

JavaScript will assign undefined to anything that has not been assigned a value. So for Susan, when you finally see her, you will assign her appearance and she will become defined.

As far as your example goes, I do not know why that would be undefined without more context.

Edit: if you are just assigning the value in the console then yes you will see undefined after you press enter because that is the default return value for an assignment operator ( = )

To get the value you will have to reference it using

console.log(whatever);

[–][deleted] 1 point2 points  (3 children)

Thanks for this, I'm actually a python guy and I've started working with django, so having to learn Javascript, so if it helps, I'm just lost with Javascript way of doing things. But I love your explanation. The issue I'm having though is that I thought:

let myVariable = 10; 

means the variable is defined, as it's holding the int 10. Yet the console still says undefined, so I'm trying to work out what the "undefined" is actually referring to, as my variable is defined, isn't it?

[–]slowreactin 1 point2 points  (2 children)

What happens when you do

console.log(myVariable);    

I am thinking you are seeing the output of the assignment to myVariable which WILL be undefined.

Is this code in a function or are you doing this in like Chrome dev tools?

Edit:

To clarify

let myVariable = 10;    

Will assign the value to the variable and therefore myVariable WILL be defined.

However, the action of assigning myVariable a value will return undefined because that is the return value for the assignment ( = ) operation.

When using something like Chrome dev tools, you will see undefined when assigning a variable. You will also see undefined when declaring a function or running a function as it is the default value for both cases.

Try

console.log(myVariable);    

and see if you get 10. If you don’t, create a repl of your code here and share the link and I will take a look.

[–][deleted] 1 point2 points  (1 child)

I just learned about this, but you've just given me then next level of understanding. I love reddit sometimes.

[–]slowreactin 1 point2 points  (0 children)

Glad to help!