Now you can also define arguments to be passed to the JavaScript function.
Now the defined arguments are defined without the keyword var.
In fact, if you place the key word var in the function argument definition,
that will be a syntax error.
If you want your function to return some value, you use the return keyword
followed by whatever value it is that you want to return.
If the return keyword is not followed by a value,
what you're signaling to the JavaScript engine is that it should
terminate the function and exit out of it without returning anything.
Now the way you pass the arguments to the JavaScript function
is also kind of unique.
So in the first line of this example here, we have the function compare that we saw
in the previous slide, and it's defined here with arguments x and y.
In the second line, we're executing or invoking function compare and
passing to it two arguments, 4 and 5.
Now the result of the execution of that function is then stored in the variable a.
On the third line, you notice that we're still executing the compare function but
we're not storing the result anywhere, and that is perfectly legal.
And it's also legal since JavaScript is a dynamically typed language
to pass a as a second argument, as a string, as opposed to a number.
Now that may not make sense as far as the logic to the function is concerned, but
it's certainly perfectly legal syntax.
Now the last line demonstrates yet another rule about JavaScript functions.
All arguments defined in a JavaScript function are optional.
You can call that same compare function without any arguments at all, and
it all will be perfectly legal.
Again, it may not make sense as far as the business logic
of the execution of the compare function.
But the syntax and the invocation of this function is perfectly legal.
Now each function or variable lives within a particular scope.
In JavaScript there's really two scopes, global scope and
function, or otherwise known as lexical scope.
Now lexical here means that it depends on where something is physically defined.
For example, a variable that's defined within a function
is physically defined within that function.
There's no block scope in JavaScript.
In other languages, curly braces themselves signify a new scope.
In JavaScript, functions signify a new scope.
That's all you really need to know about functions for now,
and we're going to talk about functions in much more detail in a later lecture.
Now variables and functions defined in a global scope are available everywhere.
Meaning any other functions that are defined in a global scope and so
on can get access to these globally defined functions and variables.
As far as function and lexical scope, variables and
functions defined here are available only within this function.
And you heard it right, you can define functions within other functions.
And those newly defined functions,
have a way to get at its outer function variables and functions.
Which leads us to the next topic, the scope chain.
Now in order to understand how scope chain works in JavaScript,
you have to understand how a JavaScript engine executes things.
Everything in JavaScript is executed in an execution context.
Now a function invocation creates a new execution context
within which that function is executed.
Now each execution context has the following.
Number one, it has its own variable environment.
This is where it stores its newly defined functions and variables.
It also gets the special this object,
which we're going to talk about at a later lecture.
And what's most important for
our discussion right now, it gets a reference to its outer environment.
Meaning the execution environment within which this particular function is defined.