Quote:
Originally Posted by jhispro97
Hey, I've just started coding and i need help. Can anyone tell me whats wrong with these lines of codes please?
Code:
var multiplied;
var timesTwo = function(number) {
var multiplied = number * 2;
};
timesTwo(4);
console.log (multiplied);
It doesnt print out the value of multiplied, instead it says that it is undefined. What have i done wrong?
|
You have two versions of the variable 'multiplied'.
One is local and will work fine if displayed within the function.
However, you are not doing that.
The second version of 'multiplied' is a global varible and is unassigned.
This is the one you are trying to display in the console log.
BTW, you second [ code] tag should have a /code to display properly.