JavaScript

Resolved: Logging to Firebug console breaks IE

The firebug console is great for debugging javascript -- but it's really annoying when you want to quickly check something in IE and can't without first removing all your debug statements to avoid a bunch of javascript errors.

Here's a handy log function which checks first that firebug is in use before trying to log to the console. The function is a no-op in IE.

log_debug = function() {
  if(window.console && window.console.firebug) console.log.apply(this, arguments)
}

log_debug('foo', 'bar', 'bash')

Declaring and initializing arrays

These are identical declarations for an array:

var foo = new Array()
var bar = []

...as are these, which also initialize the array:

var foo = new Array('first', 'second', 'third')
var bar = ['first', 'second', 'third']

Both of the above are accessed like so:

alert(foo[0])  // alerts 'first'
alert(bar[0])  // alerts 'first'

An associative array (which is really an object in JS) is declared/initialized like so:

var bash = {
  first : 'value 1',
  second: 'value 2',
  third : 'value 3'
}

Global and Local variables in JavaScript

Any variable that is initialized inside a function using the var keyword will have a local scope. If a variable is initialized inside a function without var, it will have a global scope. A local variable can have the same name as a global variable.

Syndicate content