-
Title:
03-38 Revenge Of Javascript
-
Description:
-
Now that we know how to specify grammars for well-balanced expressions
-
and arithmetic and well-balanced webpage tags in HTML,
-
we're going to return our attention to JavaScript.
-
JavaScript is actually very similar to Python.
-
Just like I showed you a formal grammar for HTML,
-
we're going to work our way up to seeing a formal grammar for JavaScript.
-
But before we get there, I just want to make sure
-
that we really understand how JavaScript programs are interpreted.
-
I'm going to show you a few more in Python and in JavaScript for comparison.
-
Over here on the left, I have a Python function
-
that computes the absolute value of its integer argument.
-
If you give me a negative number, like -5, I will return positive 5.
-
If you give me a positive number, like +9 million, I will return 9 million.
-
The return value of this function is always either a zero a positive number.
-
Now I'm going to write the same thing in JavaScript
-
to provide for a comparison.
-
Everything I've drawn in blue is a special keyword or punctuation mark used by the language.
-
For example, to define a function in Python we use "def."
-
In JavaScript, we write out the word function, but then it's still our choice what to call it--
-
I'm called it "absval" in both cases--
-
and how many parameters it should receive and what they're names are.
-
In both cases, we have one parameter named x.
-
In Python we use colons and tabbing to tell what the body of a function is,
-
what the then branch for an if is, what the else branch for an if is.
-
In JavaScript, we use curly braces and closing curly braces
-
to denote this sort of lexical or syntactic scope.
-
This is sort of curly brace 1, and it matches up with closed curly brace 1 over here,
-
2 matches up with 2, and 3 matches up with 3.
-
But in general the logical structure, the flow or the meaning, is the same.
-
In both cases we check to see if x is less than 0, and we return 0 minus x in that case,
-
or just x alone in the other case.
-
One of the most important operations in any language is printing out information,
-
displaying it to the screen so that we can see the result of computation
-
or just to help us debug.
-
In Python we use the print procedure.
-
We pass it a bunch of strings.
-
Here I'm adding together the strings "hello" and exclamation mart
-
to make a very enthusiastic greet--"hello!"
-
Over here on the right, I'm showing the same thing in JavaScript.
-
The equivalent of "print" is "document.write" or perhaps just "write."
-
In this class, we'll almost always abbreviate it down to just "write" to save space.
-
If you're familiar with object-oriented programming, which is not required for this class,
-
you might guess what the dot is about.
-
We might talk more about that later.
-
One of the key differences, though, is that all of our JavaScript functions have to have
-
these open and close parentheses like a mathematical function
-
has parentheses around its argument.