So I was trying to write a JS programme when I found out that when I call the function in the js file and run it, it gives me a different output than the output I get when I don't call the function in the js and instead run it in the console.
So I have put my code for the different scenarios below:
- When I call my function inside js file itself and then run it.
var output=[];
var count=1;
function fizzBuzz(){
output.push(count);
count=count+1;
console.log(output);
}
fizzBuzz();
- When I call the function in the console.
var output=[];
var count=1;
function fizzBuzz(){
output.push(count);
count=count+1;
console.log(output);
}
Outputs: 1) Every time I run it, it gives me the same output i.e; [1].
2)Calling for the 1st time, output= [1]
2nd time, output=[1, 2]
3rd time, output= [1, 2, 3]
and goes on.