Javascript Basics




CS174

Chris Pollett

Nov 2, 2020

Outline

General Overview

Lexicon

Example Javascript and HTML document

<html>
<head><title>test</title>
<meta name="description" value="this example illustrates how Javascripts are executed both when the document is loaded and on the occurrence of events" />
<script type="text/javascript" >
   function sayHello()
   {
      alert("hi there");
   } 
</script>
</head><body><form><input type="button" value="test" onclick="return sayHello();" 
/><!-- responds to events --></form>
<script type="text/javascript" >
for( i = 0; i<100; i++) { 
    document.writeln("<p>hi"+i+"</p>");
} // run when document loads
</script></body>
</html>

Primitives

Variables and Constants

Numeric Operators and Objects

Strings and Type Conversion

Quiz

Which of the following statements is true?

  1. WebDAV is a kind of version control software.
  2. Git repositories must be centrally hosted on GitHub.
  3. To get changes from one git repository into another we might use the command git pull.

typeof, Assignments, and the Date Object

I/O

Control Statements

Objects

Arrays

More Arrays

Functions

Function Example

function swap(i, j, a)
{
	var tmp=a[i]; /* explicitly defined variables 
                         have scope within the function
                         if I had declared the variable 
                         implicitly it would have global scope */
	a[i] = a[j]; a[j] = tmp;
}