30/09/2018, 22:21

Global variable trong javascript

Mình muốn hỏi là tại sao hai đoạn code này lại cho KQ khác nhau.

<!doctype html>
<html>
<head>
</head>
<body>
<script>
function showmoney(){
money = 98;
alert("In the function the value is "+ money);}
var money =99;
alert("In the main the value is "+ money);
showmoney();
alert("In the main the value is "+ money);
</script>
</body>
</html>
<!doctype html>
<html>
<head>
</head>
<body>
<script>
function showmoney(){
var money = 98;
alert("In the function the value is "+ money);}
var money =99;
alert("In the main the value is "+ money);
showmoney();
alert("In the main the value is "+ money);
</script>
</body>
</html>
Bài liên quan
0