Return to Video

03-38 Revenge Of Javascript

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

dummy description

more » « less
Video Language:
English
Team:
Udacity
Project:
CS262 - Programming Languages
Duration:
02:54
Amara Bot added a translation

English subtitles

Revisions