JavaScript
Two

Recap

HTML+CSS+JS

client-side

data types

string

Objects

Arrays

Functions

==/===

this

Variables

var

Operators

loosely typed

+

expr1 && expr2

Some useful things
  • window.alert("some message");
  • document.write("some text");
  • document.getElementById("testId");
  • document.getElementsByTagName("divi");
  • console.log("some text");
  • innerHTML
  • window.prompt("some message", "default");

if
            
  if (expression)
    statement
            
            
if...else
            
  if (expression)
    statement1
  else
    statement2
            
            
else if
            
  if (expression)
    statement1
  else if
    statement2
  else if
    statement3
  else
    statement4
            
            

switch
            
  switch (expression)
  {
    case condition 1: statements
                      break;

    case condition 2: statements
                      break;
        ...

    case condition n: statements
                      break;

    default: statements
}
            
            

while
          
  while (expression)
    statement
          
          
do/while
          
  do
    statement
  while (expression);
          
          

for
          
  for(initialize ; test ; increment)
    statement
          
          

for/in
          
  for (variable in object)
        statement
          
          
  • loops through the properties of an object
  • this will also loop through all the properties of all the objects that it inherits from
  • to loop through the properties of just the current object use:
  •           
      for (var name in object) {
        if (object.hasOwnProperty(name)) {
          statement
        }
      } 
              
              

    break
    causes the innermost enclosing loop or switch statement to exit immediately
    break;

    labeled break

    • to break out of a labeled statement
    • needs to be nested within the referenced label
    • labeled statement can be any block statement
    •           
        outer_block: {
          inner_block: {
            console.log('1');
            break outer_block; // breaks out of both inner_block and outer_block
            console.log('2'); // skipped
          }
          console.log('3'); // skipped
        }
                
                

    labeled break in loops
    • put label on a loop and then can break from that loop
    • useful in case of nested loops
    •           
        outer: for (var i=0; i<5; ++i){
          console.log("outer: ", i);
          inner: for(var j=0; j<5; ++j){
            console.log("inner: ", j);
            break outer;
          }
        }
                
                
    • if you forget to declare a variable, JavaScript assumes it as a global variable

    continue
    • terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration
    • labeled continue

    • continue statement needs to be nested within this labeled statement
    •           
        outer: for (var i=0; i<5; ++i){
          console.log("outer: ", i);
          inner: for(var j=0; j<5; ++j){
            console.log("inner: ", j);
            if (j == 2){
              continue outer;
            }
          }
        }
                
                

    throw
    • to signal an error or exceptional condition
    •           
        throw new Error("msg");
                
                

                
        throw {name: "exceptionName", message: "msg"}; 
                
                

    try/catch/finally
    • exception handling mechanism
    •           
        try {
          ...
        }
        catch (e) {
          ...
        }
        finally {
          ...
        }
                
                

    Creating new objects
    • with literal notation
    •           
        var obj = { property_1: value_1, // property_# may be an identifier...
                    2: value_2, // or a number...
                    // ...,
                    "property n": value_n }; // or a string
                
                

    Creating new objects
    • using a constructor function
    •           
        function MyObject(name, type) {
          this.name = name;
          this.type = type;
        }
      
        var myobj = new MyObject("Test Object", "Test Type");
                
                

    Creating new objects
    • using the Object.create() method
    • creates a new object with the specified prototype object and properties
    •           
      Object.create(proto [, propertiesObject ]);