JavaScript syntax: Difference between revisions

Content deleted Content added
Nelsonkam (talk | contribs)
Methods: Improved formatting for better rendering.
Tags: Mobile edit Mobile web edit
Nelsonkam (talk | contribs)
Functions: Improved formatting for better rendering on mobile version.
Tags: Mobile edit Mobile web edit
Line 1,145:
==Functions==
 
A [[function (programming)|function]] is a block with a (possibly empty) parameter list that is normally given a name. A function may use local variables. If you exit the function without a return statement, the value <tt>{{mono|undefined</tt>}} is returned.
 
<syntaxhighlight lang=JavaScript>
Line 1,162:
Functions are [[first class object]]s and may be assigned to other variables.
 
The number of arguments given when calling a function may not necessarily correspond to the number of arguments in the function definition; a named argument in the definition that does not have a matching argument in the call will have the value <tt>{{mono|undefined</tt>}} (that can be implicitly cast to false). Within the function, the arguments may also be accessed through the <tt>{{mono|arguments</tt>}} object; this provides access to all arguments using indices (e.g. <ttcode>arguments[0], arguments[1], ... arguments[n]</ttcode>), including those beyond the number of named arguments. (While the arguments list has a <tt>.length</tt> property, it is ''not'' an instance of <tt>{{mono|Array</tt>}}; it does not have methods such as <tt>{{mono|.slice()</tt>}}, <tt>{{mono|.sort()</tt>}}, etc.)
 
<syntaxhighlight lang=JavaScript>