Javascript Basics Part II


  • break
  • for
  • switch
  • throw
  • try
  • with

For statement

for (var name in object)
		// within the loop, name is the key of current memeber
		// object[name] is the current value

Switch statement

  • The switch value can be a string. Not restricted to integer
  • The cast statements can be expressions, not just constants

Throw statement

throw new Error(reason);

	name: exceptionName,
	message: reason

Try statement

  • Because we don’t have classes, we just have one catch clause. There cannot be multiple catch clauses.
  • The javascript implementation can produce these exception names:
    • Error
    • EvalError
    • RangeError
    • SyntaxError
    • TypeError
    • URIError



With statement

  • Ambiguous and error prone, don’t use it.
with (o)
	foo = null;

Either = null; or foo = null; depending on if foo is a global variable

Function statement

function name(parameters)


Var statement

  • Types are not specified
  • Initial values are optional


  • Blocks do not have scopes, only function has scopes.
  • Because of laziness while writing the compiler :)
  • This is a mistake in language

Return statement

return expression; Or return;

  • If there is no expression, then the return value is undefined
  • Exception in constructors, return value is this