-4

My Code (Javascript):

var num = 0;
function change(){
    var num = 10;
}
        change();
        document.write(num);

The result should be 10 but it is showing 0. Why?
If the code is wrong, what is the correct way to do this?

Hasnain Ali
  • 5
  • 1
  • 7

1 Answers1

0

You are setting another local variable called num.

Just change the inner var num to num.

Qontrol
  • 97
  • 6