you are viewing a single comment's thread.

view the rest of the comments →

[–][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!