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