AL-zami AL-zami - 10 days ago 6
Javascript Question

Two confusing statements, which one to use and why?

I am confused about these two statements. Both are giving two different results. For simplification I am not posting full code here. The key point is I want to know the difference between the two. say, I want to invoke a function when I scroll the page. What is the difference between using a parentheses and not using a parentheses in this statement?

window.onscroll=function_Name;


And

window.onscroll=function_Name();

Answer

With parenthesis means: run the function and return the result.

Without parenthesis means: use the function itself.

So if you want to bind a function to the "onscroll" event, you do NOT want parenthesis. For example, if your function returns "2", then

 window.onscroll=function_Name();

would be the same as

window.onscroll="2";

which wouldn't make sense.

Comments