CS110Introductionsyllabusassignmentsdocumentationprojectsite map

  

  

  JavaScript Quick Reference

  [ Data Types | Variables | Operators | Comparison ]
  [ Statements | Built-In Functions | User-Defined Functions ]


  Data Types:

    Data Type Examples
    Number 1
    1024
    18.0
    3.14159
    1.024e3
    String ""
    "foo"
    "2 words"
    Boolean true
    false

  Variables:

    A variable is a symbolic name for an unspecified value in an expression or statement. A variable can be used in any situation where the data that it refers to can be used. A variable name can refer to any of the data types, described above. Variables are given values by using the assignment operator as described below. All of the examples below make use of variables (e.g. x, y, Str, flag, etc...)

    The following lists identify variable names that should be avoided because they are already used by JavaScript or Netscape.

    JavaScript Reserved Words
                                                
    break          continue       delete         else        
    false          for            function       if              
    in             new            null           return
    this           true           typeof         var         
    void           while          with
    
    Java Keywords Reserved by JavaScript
    absract        boolean        byte           case
    char           class          const          default
    do             double         extends        final
    finally        float          goto           implements
    import         instanceof     int            interface
    long           native         package        private
    protected      public         short          static
    super          switch         synchronized   throw
    throws         transient      try
    
    Other Variable Names to Avoid
    alert          Anchor         Area           Array
    assign         blur           Boolean        Button
    Checkbox       clearTimeout   close          closed
    confirm        Date           defaultStatus  Document
    document       Element        escape         eval
    FileUpload     focus          Form           Frame
    frames         Function       getClass       Hidden
    History        Image          isNaN          java
    JavaArray      JavaClass      JavaObject     JavaPackage
    length         Link           Location       location
    Math           MimeType       name           navigate
    Navigator      netscape       Number         Object
    onblur         onerror        onfocus        onload
    onunload       open           opener         Option
    Packages       parent         parseFloat     parseInt
    Password       Plugin         prompt         prototype
    Radio          ref            Reset          scroll
    Select         self           setTimeout     status  
    String         Submit         sun            taint
    Text           Textarea       top            toString
    unescape       untaint        valueOf        Window
    window
    
      Source: JavaScript the Definitive Guide. David Flanagan, O'Reilly & Associates, Inc. 1997.

  Operators:

    Operator Name Data Types Examples Result
    = Assignment Number
    String
    Boolean
    x = 3;
    y = 7.654;
    Str = "foo";
    flag = true;
    x == 3
    y == 7.654
    Str == "foo"
    flag == true
    + Addition or
    Concatenation
    Number
    String
    x = 4 + 3;
    y = x + 7.432;
    Str = "foo" + "bar";
    x == 7
    y == 14.432
    Str == "foobar"
    - Subtraction Number x = 7 - 2;
    y = x - 3.359;
    w == 5
    y == 1.641
    * Multiplication Number x = 2 * 5;
    y = x * 9.773;
    x == 10
    y == 97.73
    / Division Number x = 10 / 4;
    y = x / 8;
    x == 2.5
    y == .3125
    % Remainder1 Number x = 10 % 4;
    y = x % .75;
    x == 2
    y == .5
    && AND Boolean flag = true;
    D = true && flag;
    D = D && false;
    flag == true
    D == true
    D == false
    || OR Boolean flag = true;
    D = flag || true;
    D = false || D;
    flag == true
    D == true
    D == false
    ! NOT Boolean D = !true;
    D = !D;
    D == false
    D == true
    1. The Remainder operator is often referred to as the Modulo operator.

  Comparison:

    Operator Name Data Types Examples
    == Equality Number
    String
    Boolean
    3 == 3
    "test" == "test"
    false == false
    != Inequality Number
    String
    Boolean
    12 != 39
    "foo" != "bar"
    false != true
    < Less Than Number
    String1
    2 < 3.1415
    "a" < "b"
    <= Less Than or Equal Number
    String1
    17 <= 17
    "ABC" <= "ABD"
    > Greater Than Number
    String1
    12 > -4
    "foo" > "bar"
    >= Greater Than or Equal Number
    String1
    3.99 >= 3.98
    "fool" >= "foo"
    1. Strings follow standard dictionary ordering, i.e.,
        "A" < "B" < ... < "Z" < "a" < "b" ... < "z"
        "a" < "aa" < ... < "ab" < "aba" < ... < "b"

  Statements:

    Statement Name General Form Examples
    Comment
    // Comment...
    
    /* Comment... */
    
    // A one line comment.
    
    /* A comment that goes onto 
    more than one line */
    
    If-Then Statement
    if (test) {
        if-clause
    } 
    else {
        else-clause
    }
    

    test:
    Evaluates to true or false.
    if-clause:
    Executed if test==true.
    else-clause:
    Execued if test==false. The else-clause is optional.
    // add 3 to x if non-neg.
    
    if (x >= 0) {
        x = x + 3;
    }
    
    
    
    // decrement x if neg.,
    //   otherwise increment
    
    if (x < 0) {
        x = x - 1;
    }
    else {
        x = x + 1;
    }
    
    While Loop
    while (test) {
        loop-body
    }
    

    test:
    Evaluates to true or false. If test==true the loop body is executed and the test is performed again.
    // sum = x+(x-1)+...+3+2+1
    
    sum = 0;
    while (x >= 1) {
        sum = sum + x;
        x = x - 1;
    }
    
    For Loop
    for (init; test; action) {
        loop-body
    }
    

    init:
    Evaluated once when the for loop is first reached.
    test:
    If test==true the loop body will execute.
    action:
    Executed after the loop body and before the next evaluation of test.
    // sum = 1+2+3+...+x
    
    sum = 0;
    for (i = 1; i <= x; i = i + 1) {
        sum = sum + i;
    }
    
    
    
    // fact = 1*2*3*...*10
    
    fact = 1;
    for (j = 2; j <= 10; j = j + 1) {
        fact = fact * j;
    }
    

  Built In Functions:

    Function Examples Result
    Square Root x = Math.sqrt(9); x == 3
    Absolute Value x = Math.abs(-23);
    y = Math.abs(99);
    x == 23
    y == 99
    Maximum x = Math.max(3,14); x == 14
    Minimum x = Math.min(3,14); x == 3
    Power x = Math.pow(2,3); x == 8
    Floor x = Math.floor(3.14159);
    y = Math.floor(4.5623);
    x == 3
    y == 4
    Ceiling x = Math.ceil(3.14159);
    y = Math.ceil(4.5623);
    x == 4
    y == 5
    Round x = Math.round(3.14159);
    y = Math.round(4.5623);
    x == 3
    y == 5
    ParseFloat x = parseFloat("12.8");
    y = parseFloat("ABC");
    x == 12.8
    y == NaN
    Prompt theName = prompt("Enter Your Name:","Default Value");

    Netscape will display the following dialog box, and theName will be assigned the value that is entered in the text field.

    Write document.write("Howdy");
    document.write(2+3);
    displays "Howdy"
    displays 5

  User Defined Functions

    Action General Form Examples
    Function Definition
    function FuncName(param1, param2,...) 
    {
        variable-declarations
    
        function-body
        
        return return-value;
    }
    

    FuncName:
    The name of the function.
    param1:
    The name of the first parameter passed to the function.
    variable-declarations:
    list of local variables (if any) that appear in the function, preceded by var.
    function-body:
    JavaScript statements to be executed when the function is called.
    return-value:
    Value that is returned from this function. The return statement is optional.
    
    function FooFunc() 
    // Returns:  the string "foo"
    {
        return "foo";
    }
    
    
    
    function BarFunc(N)
    // Assumes: N >= 0
    // Returns: string of N "bar"s
    {
        var i, theStr;
    
        theStr = "";
        for (i = 1; i <= N; i = i + 1) {
            theStr = theStr+"bar";
        }
        return theStr;
    }
    
    
    function Greet(name)
    // Assumes: name is a string
    // Results: displays greeting
    {
        document.write("Hi "+name);
    }
    
    Function Call
    myVar = FuncName(arg1, arg2,...);
    

    myVar:
    Variable to hold the value (if any) returned by the function.
    FuncName:
    Name of the function to call.
    arg1:
    Argument 1, the value for the first parameter of the function.
    Str1 = FooFunc();
    
    // Result: Str1 == "foo" 
    
    
    
    Str2 = BarFunc(3);
    
    // Result: Str2 == "barbarbar"
    
    
    
    Greet("Oliver");
    
    // Result: displays "Hi Oliver"