Why is this correct?


#1


Well, the code runs fine. However I don't understand how the function can have nothing in the ( ) and works.


var count=0

var loop = function(){
	while(count<3){
		//Your code goes here!
		console.log("I'm looping!")
		count++
	    
	}
}

loop();


#2

function can also have no arguments? it is fine, a function can have a argument:

var example = function(argument)

or not a argument:

var example = function(argument)

both is perfectly fine.


#3

really?
i always thought that (gonna use this current code as an example):

when we put loop(count), it will be used in function(count), and thus whatever the value count was, the value would be used in the resulting block of code of function() in the {} like this: while(count<3)


#4

so i guess what i said is what will happen, but when there isnt any arguments, how does that work??


#5

this code will both work with and without function. Well, simple? Function can manipulate global variable, so the global variable count will simply be used.


#6

ok, then how do we know when to not have an argument in function?


#7

oh wait i think i get your last statement, basically if there isnt anything betw the function(in here), the code in the block will use the global variable; if any global variable corresponds to the variable inside is it?


#8

Sometimes arguments are really useful (when your function must do different things, you can pass arguments to determine this). There is no fixed rule for this

function always have the ability to access/manipulate global variable, the function parameter doesn't matter for this


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.