The real answer to your question is to learn how to debug JavaScript code. Your web browser has a debugger built in that will let you step through your code statement by statement and view all your variables each step of the way. Please try this; it will help you immensely.
Simply add a debugger; statement before the first line of code, then open the developer tools and reload your page. The debugger will stop on that statement, and then use the Step In button in the debugger to single step through the code.
To make it easy for you to get started, here is a copy of your original code (with the bug) as a snippet with the debugger; statement added:
function test() {
debugger;
array = [4, 5, 1, 3]
for (var i = 0; i < array.length; i++) {
var num = 0;
if (array[i]> num) {
num = array[i];
}
}
}
test();
It's best to put the code you're testing inside a function as I did here. That lets you look at your local variables without the standard global variables and functions cluttering the view.
Open the developer tools and then run the snippet, then use the single step button as I mentioned above.
Every browser has developer tools built in that work in a similar way. For example, here is a guide to the Chrome DevTools.
Math.max(...array);orarray.reduce((acc,itm)=>itm>acc?itm:acc,0)