I have following code:
module = {
submodule: {
value: 'foo',
init: function(){
console.log(module.submodule.value);
}
}
};
When I’m testing this code through console I get correct value and undefined but when I’m using same code with return statement inside init function I only get correct value. I suppose this is one of those common beginner and trivial questions but I currently can’t wrap my head around this 🙂
Your function isn’t returning anything. THe console only displays the returned value of the function. There is no need for your function to return something, but if you want the console to finally show “value”, then use
What happens is that each function “returns” something. This something can be used as inputs elsewhere. For example,
You can do stuff like
But, if you decide to not let your function return something (or use
return;since it can immediately exit the function), it will return an undefined value, since, well, you didn’t define what you wanted it to return. WHich is fine, as long as you don’t try to use the returned value.