- Function: A function is a block of code that performs an action and returns a result; optionally takes arguments
Comment: anything following
alert()opens a dialog box and returns
confirm()opens a dialog box and returns a boolean.
prompt()opens a dialog box and returns a string
An argument is what you pass into a function or method; a parameter is a variable that's assigned to the argument. In the example below,
number2 are parameters while
2 are arguments.
P*arts of a function*
- A function declaration starts with the
- Next comes the name. It should be descriptive and clear. In the example above, the name is
add. Always use lower camel case for function names.
- The name is always followed with parens. These parens may or may not include parameters. In the
- The function body is enclosed in curly braces. The code inside the curly braces is the code that will be executed when the function is called.
- Generally, you will want to
returna value from your function. It's very common for beginners to forget the
return, which means that the function will return
undefinedinstead of a value. In the example above, the
addfunction will return the value of
number1 + number2.
Top comments (0)