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