Why it prints out an "undefined" when I declare the local variable after the console.log


#1

Hello there, I'm learning "Introduction to Functions in JS", lesson #10. Global vs Local Variables, wondering why it prints out an "undefined" when I declare the local variable after the console.log, sample code as below:

var my_number = 7; //this has global scope

var timesTwo = function(number) {
   
    console.log("Inside the function my_number is: ");
    console.log(my_number);
    var my_number = number * 2;
}; 

timesTwo(7);

console.log("Outside the function my_number is: ")
console.log(my_number);

#2

This topic was automatically closed after 60 minutes. New replies are no longer allowed.