Javascript - Why Functions Vs Code Blocks are Needed Sometimes -



Javascript - Why Functions Vs Code Blocks are Needed Sometimes -

okay, way explain example. in javascript can such as:

if(condition) { }

the { , } used identify code block. in cases such this:

window.onload = function() { }

you need function() included. why can't be:

window.onload { }

why need function() ? also, thought functions have name associated them, , when name called function runs, why in case there function no associated name?

if, for, while, try, catch, etc. statements modify next statement. example, if (condition) statement execute next statement if condition look evaluates true (or truthy). block statement special type of statement used grouping multiple statements together, allowing if statement apply larger section of code.

however, window.onload = function() { } expression—an entirely different construct. it's assignment look consisting of 3 parts, assignment operator (=) look representing value assign (in case function expression) , reference variable or property assign value (window.onload).

note there difference between function expression mentioned above , function statement, can lead confusion. in both constructs, curly brackets required around body (unlike in if statement).

javascript

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -