JavaScript Basics

Monday February 19


Programming Basics

Program

Formal Language

Program Execution

Data

Data Types

Numbers

Numbers in other Bases

Strings

Special Characters

Boolean

Type Checking

Variables

Variable Names

Objects

Expressions & Operators

Mathematical Operators

Comparison Operators

Boolean Operators

Assignment Statement

Assignment Statements

      <SCRIPT LANGUAGE="JavaScript">
      var x, Name1, Name2, Name, Square;
      x = 5;
      Name1 = "George";
      Name2= "Washington";
      Name = Name1 + Name2;
      x = x + 2;
      Square = x * x;
      </SCRIPT>
      

Loose Typing

      var x;
      x = 23;
      x = "a string";
      x = x + 5;
      

Declarations & Scope

Output

document.write()

      <SCRIPT LANGUAGE="JavaScript">
      var Name1, Name2, Name;
      Name1 = "Joe";
      Name2= "Bob";
      document.write(Name1, Name2);
      document.write(Name1+Name2);
      document.write(' says "Howdy" ');
      document.write("says \"Howdy\" ");
      document.write("says " + ' "Howdy" ');
      </SCRIPT>
      

JavaScript Comments

Statements

Block of Statements

Conditional Statement

IF statement

   if (condition)
      statement;
   
   if (num > max)
      max = num;
   z = 23;
   

IF-ELSE statement

   if (condition)
      statement;
   else
      statement;
   
   if ( x > y)
      max = x;
   else
      max = y;
   

Multi-way choice

   if (condition 1)
      statement 1;
   else 
   if (condition 2)
      statement 2;
   else 
   if (condition 3)
      statement 3;
   else
      statement 4;
   

Iteration

For Loop

   for (initialize_expr ; condition; increment_expr)
      statement;
  
   sum = 0; 
   for ( i = 0; i < 10; i = i + 1)
      sum = sum + i;
   document.write("Sum is " + sum);
   

While Loop

   while (condition)
      statement;
   
   sum = 0; 
   i = 0;
   while (i < 10)
   {
      sum = sum + i;
      i = i + 1;
   }
   document.write("Sum is " + sum);
   

Loops


Examples