Skip to content
4thex edited this page Oct 16, 2014 · 61 revisions

JavaScript

What is it?

JavaScript is a programming language. Even though the name contains Java, it has little to do with this programming language.

Why should I care?

JavaScript is the programming language of a Chrome Application. It is JavaScript that is executed when we use the mouse or the keyboard to interact with our application.

Statements

Statements describe things to do. Each statement is ended with a semi-colon (;). Multiple statements can be combined into a set of curly brackets {} called a block statement. A block statement is not ended with a semi-colon.

statement1;
statement2;
{
  statement3;
  statement4;
}

Please note that block statements do not provide a scope. In other languages such as Java, something inside curly brackets are considered private to that area, but this is not the case in JavaScript

Variable Statement

Variables must be declared with the var keyword followed by the name of the variable. You can optionally also assign a value when you declare variables. Once declared, the var keyword must not be used.

var some;
var other = 5;
other = 6;

Names of variables must not be one of the keywords or built-in global variables.

If...else Statement

The if...else statement is used to execute some statements if a condition is true and something else if it is false.

var a = 5;
if(a < 6) {
  statement1;
  statement2;
} else {
  statement3;
  statement4;
}

Since the variable a has the value 5 which is less than 6, statements statement1 and statement2 will be executed.

The following values are considered false:

undefined, null, 0, NaN, ""

Everything else is considered true.

For Statement

The for statement creates a loop. The statement takes three expressions separated by semicolon enclosed in parentheses followed by the statement to execute.

The first expressions is the initial statement to execute.

Then comes a condition that has to be true in order for the loop to continue.

The final expression is the statement to execute after each iteration of the loop.

for(initialStatement; condition; afterIteration)
  loopStatement

So the order of execution is:

  1. initialStatement
  2. condition
  3. loopStatement
  4. afterIteration
  5. condition

Steps 3-5 will be repeated as long as the condition is true.

Example:

for(var i=1; i<10; i++) {
  console.log(i);
}

Objects

An object in JavaScript

Loading from an HTML5 page

A JavaScript file is loaded from an HTML5 page using the script element.

<html>
  <head>
    <script type="text/javascript" src="background.js">Your browser does not support JavaScript</script>
  </head>
  <body>
  </body>
</html>

The type attribute indicates the type of script. It is necessary to specify this because browsers support other script languages. Internet Explorer for example has its own types; JScript, and VBScript.

The src attribute gives the name of the file that contains the JavaScript source code.

Where can I find more information?

Mozilla JavaScript Reference

Introduction
[Install CDE](Install CDE)

Part 1 - The basics

HTML5
CSS
JavaScript
DOM

Part 2 - Projects

[A Ringing Bell](A Ringing Bell)
[Projectile Movement](Projectile Movement)
[Map with location](Map with location)
[Slide 15 Puzzle](Slide 15 Puzzle)
[A Running Man](A Running Man)
Sudoku

Part 3 - Getting it out there

[Package Chrome Application](Package Chrome Application)
[Uploading Your Application](Uploading Your Application)

Clone this wiki locally