Console Learner — An interactive JavaScript course.
3.1 Named Functions
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.
Any number of arguments can be supplied in the function call. Either
too few and too many. It is therefore prudent to check the input
arguments for errors or invalid types.
Return values are optional in JavaScript. The default return
value is always undefined.
Side-effects are common in JavaScript functions, such as web page
modifications or similar. There is no distinction between
functions with side-effects and those without.
>>>
function hello(name) {
return "Hello, " + name + "!";
}
A function with a single argument. The arguments are variables visible
only inside the function. A return statement
stops execution and immediately returns a value.
>>>
hello('world')
"Hello, world!"
A function is called with the name followed by a parenthesized
list of argument values. Each argument is separated with a comma,
e.g. (arg1, arg2, ...).
>>>
hello()
"Hello, undefined!"
Any number of arguments can be provided in a function call. The
declared arguments without a value are initiated to
undefined.
>>>
function add(a, b) {
return a + b;
}
>>>
add(1, 2)
3
>>>
add('1', '2')
"12"
As the add() function performed no type checking,
it can be re-purposed for concatenating strings...
>>>
The console allows you to interact with the course material and
examples. Use the following keys:
<Enter> — Execute the code
<Shift + Enter> — Add
line break
<Arrow-Up> — Previous
command in history
<Arrow-Down> — Next command in history
A special logging function is also available:
print(value) — Prints a value to
the console.
print(value, true) — Prints a debug
version of a value.
Need more magic? Check out the other
developer tools
on this site.