Docsity

JavaScript for Python Programmers: Similarities and Differences, Slides of Javascript programming

Partial preview of the text

Download JavaScript for Python Programmers: Similarities and Differences and more Slides Javascript programming in PDF only on Docsity!

Introduction to JavaScript for

Python Programmers

Lecture Outline

  • How Python & JavaScript are similar
  • How Python & Javascript are different
  • JavaScript fundamentals
  • JavaScript Examples

Python & Javascript: Differences

  • Different web browsers support different JavaScript code.
    • The basic syntax works on all browsers, but some browsers support non- standard extensions that are not supported on other browsers.
    • The source code can be stored inside of a web page, sometimes intermixed with HTML.
    • Web browsers typically do not provide good debugging information about why a JavaScript fails, so debugging a non-working script can be difficult.
  • The Syntax of Python & Javascript are different
    • Statements can be terminated with semi-colons.
    • Brackets must be used to indicate blocks.

Javascript in a Web Page

  • JavaScript is separated from HTML by putting it inside of special script tags:

  • Some javascript is actually part of HTML, for example, a function call on a button press: onClick=”cwCalc();”

  • Output from a JavaScript program usually is shown to the user as HTML that is rendered by the web browser.

Javascript: Indentation does NOT matter!

  • This: function myFunc(aVar) { if (aVar == 'test') { return( 0 ) } else { return (- 1 ) } }
  • functions the same as this: function myFunc(aVar) { if (aVar == 'test') { return( 0 ) } else { return (- 1 ) } }

But please use good indentation!

  • Improves readability and understandability function myFunc(aVar) { if (aVar == 'test') { return( 0 ) } else { return (- 1 ) } }

Declaring Variables and Arrays

  • Variables must be declared before being used: var VARIABLENAME = VALUE var myName = “Jay”
  • The “VAR” keyword tells the JavaScript interpreter that you are making a variable.
  • Arrays must have their size declared at time of creation: var ARRAYNAME = new Array(SIZE) var myArray = new Array( 50 )
  • You index elements in an array using bracket notation: var oneElement = myArray[ 1 ]

Conditional (IF) Statements

  • Very similar in Python & JavaScript.
  • IF and IF/ELSE are the same (except for brackets). No ELIF statement.
  • “Weird Conditionals”: I won't talk about them, if you run into them, do a quick web search to figure out how they work. - Switch/case/break - ?:

Looping (for) statement

  • FOR loops in JavaScript are based on C and C++ syntax, and are usually quite different than in Python. for( INITIALIZE; BOOLEAN EXPRESSION; ACTION) { STATEMENT; STATEMENT; }
  • for (var j = 0 ; j < 5 ; j = j+ 1 ) { document.write(“j is: “ + j) }

Looping (for) statement

  • FOR loops in JavaScript are based on C and C++ syntax, and are usually quite different than in Python. for( INITIALIZE; BOOLEAN EXPRESSION; ACTION) { STATEMENT; }
  • Code before the first semicolon (the initialization code) is executed once at the beginning of the loop.
  • The boolean expression (in the middle) is evaluated every time the loop repeats, and the statements in the loop are only executed if the result is TRUE.
  • The action code is executed after the loop executes, and is typically used to increment a counter.

Looping (for) Statement – Iteration over Properties

  • JavaScript does have a FOR/IN version of the for loop, but it doesn't work exactly as you'd expect from your experience with Python's FOR loops.
  • for (VARNAME in OBJECT) { document.write( VARNAME ) document.write( OBJECT[VARNAME] ) }
  • Note that it iterates through the property names of an object, and you have to use bracket notation to get the property values.

Input / Output with JavaScript

  • As most JavaScript runs inside of a web browser, most of the input and output to and from a JavaScript program goes through the web-browser.
  • Input comes from the user via web forms, button and link clicks.
  • Output is typically is displayed as a webpage or part of a webpage as HTML rendered by the web browser.

Webpage Output with JavaScript

  • A script can output calculated and dynamic information:
Clock Example:

Dialog Output with JavaScript

  • A script can use the alert( “a string”) function to pop up an “Alert” Dialog box.
Example:
  • This javascript code will be executed when it is encountered, and causes an alert-box to appear.