javascript interactive

JavaScript Interactive — a console crash course


3.1 Named Functions

Warning: Your browser may be outdated or unsupported.

The JavaScript console is known to display some of the examples incorrectly in Internet Explorer 8 or older.

The following terms will be used henceforth:

  • Function – A named and separated block of code.
  • Function Call – An expression that initiates the function execution.
  • Argument – An input parameter in a function call.
  • Return Value – A returned result from a function call.
  • Side-Effect – A state modification that persists after the function call.
>>>
function test(value) {
    print(value);
}

  
A function declaration of the simple test() function with a single value argument. The function arguments are local variables that are bound to the argument value supplied in the function call. If no value is supplied, the variable will be set to undefined.
>>>
test('Hello functions!')
undefined
A function is called with the name followed by a parenthesized list of argument values. Each argument value expression is separated by a comma, e.g. (arg1, arg2, ...). The default return value for a function is undefined.
>>>
function sum(a, b) {
    return a + b;
}

  
A return value is declared with the return statement. The function execution will then immediately stop, and control returns to the point of the function call.
>>>
sum(1, 2)
3
>>>
sum('1', '2')
'12'
As the sum() function performed no type checking, it is also possible to use it for other purposes...
>>>

Index 1 2 3 4 5 6 7 8 9