[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.00,0:00:05.00,Default,,0000,0000,0000,,Now that we know how to specify grammars for well-balanced expressions Dialogue: 0,0:00:05.00,0:00:09.00,Default,,0000,0000,0000,,and arithmetic and well-balanced webpage tags in HTML, Dialogue: 0,0:00:09.00,0:00:13.00,Default,,0000,0000,0000,,we're going to return our attention to JavaScript. Dialogue: 0,0:00:13.00,0:00:17.00,Default,,0000,0000,0000,,JavaScript is actually very similar to Python. Dialogue: 0,0:00:17.00,0:00:20.00,Default,,0000,0000,0000,,Just like I showed you a formal grammar for HTML, Dialogue: 0,0:00:20.00,0:00:24.00,Default,,0000,0000,0000,,we're going to work our way up to seeing a formal grammar for JavaScript. Dialogue: 0,0:00:24.00,0:00:27.00,Default,,0000,0000,0000,,But before we get there, I just want to make sure Dialogue: 0,0:00:27.00,0:00:30.00,Default,,0000,0000,0000,,that we really understand how JavaScript programs are interpreted. Dialogue: 0,0:00:30.00,0:00:35.00,Default,,0000,0000,0000,,I'm going to show you a few more in Python and in JavaScript for comparison. Dialogue: 0,0:00:35.00,0:00:38.00,Default,,0000,0000,0000,,Over here on the left, I have a Python function Dialogue: 0,0:00:38.00,0:00:41.00,Default,,0000,0000,0000,,that computes the absolute value of its integer argument. Dialogue: 0,0:00:41.00,0:00:46.00,Default,,0000,0000,0000,,If you give me a negative number, like -5, I will return positive 5. Dialogue: 0,0:00:46.00,0:00:51.00,Default,,0000,0000,0000,,If you give me a positive number, like +9 million, I will return 9 million. Dialogue: 0,0:00:51.00,0:00:55.00,Default,,0000,0000,0000,,The return value of this function is always either a zero a positive number. Dialogue: 0,0:00:55.00,0:00:59.00,Default,,0000,0000,0000,,Now I'm going to write the same thing in JavaScript Dialogue: 0,0:00:59.00,0:01:01.00,Default,,0000,0000,0000,,to provide for a comparison. Dialogue: 0,0:01:01.00,0:01:07.00,Default,,0000,0000,0000,,Everything I've drawn in blue is a special keyword or punctuation mark used by the language. Dialogue: 0,0:01:07.00,0:01:11.00,Default,,0000,0000,0000,,For example, to define a function in Python we use "def." Dialogue: 0,0:01:11.00,0:01:16.00,Default,,0000,0000,0000,,In JavaScript, we write out the word function, but then it's still our choice what to call it-- Dialogue: 0,0:01:16.00,0:01:18.00,Default,,0000,0000,0000,,I'm called it "absval" in both cases-- Dialogue: 0,0:01:18.00,0:01:21.00,Default,,0000,0000,0000,,and how many parameters it should receive and what they're names are. Dialogue: 0,0:01:21.00,0:01:24.00,Default,,0000,0000,0000,,In both cases, we have one parameter named x. Dialogue: 0,0:01:24.00,0:01:29.00,Default,,0000,0000,0000,,In Python we use colons and tabbing to tell what the body of a function is, Dialogue: 0,0:01:29.00,0:01:33.00,Default,,0000,0000,0000,,what the then branch for an if is, what the else branch for an if is. Dialogue: 0,0:01:33.00,0:01:38.00,Default,,0000,0000,0000,,In JavaScript, we use curly braces and closing curly braces Dialogue: 0,0:01:38.00,0:01:42.00,Default,,0000,0000,0000,,to denote this sort of lexical or syntactic scope. Dialogue: 0,0:01:42.00,0:01:46.00,Default,,0000,0000,0000,,This is sort of curly brace 1, and it matches up with closed curly brace 1 over here, Dialogue: 0,0:01:46.00,0:01:50.00,Default,,0000,0000,0000,,2 matches up with 2, and 3 matches up with 3. Dialogue: 0,0:01:50.00,0:01:55.00,Default,,0000,0000,0000,,But in general the logical structure, the flow or the meaning, is the same. Dialogue: 0,0:01:55.00,0:01:59.00,Default,,0000,0000,0000,,In both cases we check to see if x is less than 0, and we return 0 minus x in that case, Dialogue: 0,0:01:59.00,0:02:02.00,Default,,0000,0000,0000,,or just x alone in the other case. Dialogue: 0,0:02:02.00,0:02:07.00,Default,,0000,0000,0000,,One of the most important operations in any language is printing out information, Dialogue: 0,0:02:07.00,0:02:11.00,Default,,0000,0000,0000,,displaying it to the screen so that we can see the result of computation Dialogue: 0,0:02:11.00,0:02:13.00,Default,,0000,0000,0000,,or just to help us debug. Dialogue: 0,0:02:13.00,0:02:15.00,Default,,0000,0000,0000,,In Python we use the print procedure. Dialogue: 0,0:02:15.00,0:02:17.00,Default,,0000,0000,0000,,We pass it a bunch of strings. Dialogue: 0,0:02:17.00,0:02:20.00,Default,,0000,0000,0000,,Here I'm adding together the strings "hello" and exclamation mart Dialogue: 0,0:02:20.00,0:02:23.00,Default,,0000,0000,0000,,to make a very enthusiastic greet--"hello!" Dialogue: 0,0:02:23.00,0:02:26.00,Default,,0000,0000,0000,,Over here on the right, I'm showing the same thing in JavaScript. Dialogue: 0,0:02:26.00,0:02:31.00,Default,,0000,0000,0000,,The equivalent of "print" is "document.write" or perhaps just "write." Dialogue: 0,0:02:31.00,0:02:35.00,Default,,0000,0000,0000,,In this class, we'll almost always abbreviate it down to just "write" to save space. Dialogue: 0,0:02:35.00,0:02:39.00,Default,,0000,0000,0000,,If you're familiar with object-oriented programming, which is not required for this class, Dialogue: 0,0:02:39.00,0:02:41.00,Default,,0000,0000,0000,,you might guess what the dot is about. Dialogue: 0,0:02:41.00,0:02:43.00,Default,,0000,0000,0000,,We might talk more about that later. Dialogue: 0,0:02:43.00,0:02:48.00,Default,,0000,0000,0000,,One of the key differences, though, is that all of our JavaScript functions have to have Dialogue: 0,0:02:48.00,0:02:51.00,Default,,0000,0000,0000,,these open and close parentheses like a mathematical function Dialogue: 0,0:02:51.00,9:59:59.99,Default,,0000,0000,0000,,has parentheses around its argument.