As someone diving into the world of Javascript after working extensively with server-side languages like PHP, I've noticed a peculiar issue. It seems that I am unable to access variables defined outside of a function from within the function itself. To circumvent this problem, I find myself constantly duplicating these variables inside the function. Let me illustrate this with an example:
var num1 = document.getElementById("num1");
var num2 = document.getElementById("num2");
function calculateSum() {
alert(Number(num1.value) + Number(num2.value));
}