Basic Operators

Basic operators allow you to manipulate values.

Example 2.5: Concatenation
var foo = ‘hello’;
var bar = ‘world’;

console.log(foo + ‘ ‘ + bar); // ‘hello world’

Example 2.6: Multiplication and division
2 * 3;
2 / 3;

Example 2.7: Incrementing and decrementing
var i = 1;

var j = ++i; // pre-increment: j equals 2; i equals 2
var k = i++; // post-increment: k equals 2; i equals 3


JavaScript Basics

Syntax Basics

Understanding statements, variable naming, whitespace, and other basic JavaScript syntax.

Example 2.1: A simple variable declaration
var foo = ‘hello world’;

Example 2.2: Whitespace has no meaning outside of quotation marks
var foo = ‘hello world’;

Example 2.3: Parentheses indicate precedence
2 * 3 + 5; // returns 11; multiplication happens first
2 * (3 + 5); // returns 16; addition happens first

Example 2.4: Tabs enhance readability, but have no special meaning
var foo = function() {

JavaScript Debugging

A debugging tool is essential for JavaScript development. Firefox provides a debugger via the Firebug extension; Safari and Chrome provide built-in consoles.

Each console offers:

  • single- and multi-line editors for experimenting with JavaScript
  • an inspector for looking at the generated source of your page
  • a Network or Resources view, to examine network requests

When you are writing JavaScript code, you can use the following methods to send messages to the console:

  • console.log() for sending general log messages
  • console.dir() for logging a browseable object
  • console.warn() for logging warnings
  • console.error() for logging error messages

Other console methods are also available, though they may differ from one browser to another. The consoles also provide the ability to set break points and watch expressions in your code for debugging purposes.