Child pages
  • JavaScript Basics

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

stoc

Table of Contents
styleupper-roman

Introduction to JavaScript

JavaScript was originally developed by Brendan Eichof Netscape under the name Mocha, which was later renamed to LiveScript and finally to JavaScript. JavaScript code, much like other programming languages, is made up of statements which serve to make assignments, compare values and execute other sections of code.

...

JavaScript ignores those characters that take up space on the screen without visual representation or without necessary meaning.

For example:

Code Block

x = x + 1

Can be written like:

Code Block

x=x+1

or:

Code Block

x = x + 1

Whitespace not ignored (exception)

However, most operations other than simple arithmetic functions require a space to make their meaning clear.

For example:

Code Block

s = typeof x

and:

Code Block

s = typeofx

do not have the same meaning. The first statement invokes the typeof operator on a variable x and places the result in s. The second statement copies the value of a variable called typeofx into s.


Whitespaces within a string, between single or double quotes, are preserved. For example:

Code Block

var s = 'This spacing is      preserved.';

...

Single statement

Example:

Code Block

x = x + 1;

Multiple statements on one line

Example:

Code Block

x = x + 1; y = y + 1; z = 0;

Multiple statements on multiple lines

Example:

Code Block

x = x + 1;
y = y - 1;
Note
titleNote

You do not need to use a semicolon for the last statement of a method (script).

...

Here an example of statements that make up the body of a function:

Code Block

function add(x, y) {
	var result = x + y;
	return result;
}

Here is an example of more than one statement to be executed:

Code Block

if (x > 10) {
	x = 0;
	y = 10;
}

...

Here is an if else example to show standard identing in JavaScript code:

Code Block

var x = 5;
if (x > 1) {
	if (x > 2) {
		alert('x > 2');
		alert('Yes, x > 2');
	}
} else {
	alert('x <= 1');
	alert('Yes, x <= 1');
}
alert('Moving on.');

...

Single line comments begin with a double forward slash //. The interpreter will ignore everything from that point until the end of the line.

Example:

Code Block

var count = 10; // holds a number of items

...

Multiple line comments (similar to C programming) are enclosed between forward slash and asterix / at the beginning of the comment and an asterix and forward slash / at the end of the comment. Everything in between is ignored by the interpreter.

Example:

Code Block

/* Both x = 0 and y = 10
   statements will be executed
   when the if statement is true.
*/
if (x > 10) {
	x = 0;
	y = 10;
}

...

the keyword new enables you to create your own object and is used for an object's constructor.

Example:

Code Block

var myArray = new Array();
myArray[0] = "Thomas";
myArray[1] = "Bob";

...

The keyword var is used to declare variables.

Example:

Code Block

var x = 2;

JavaScript grammar

...

This example shows how to declare a variable without assigning a value to it:

Code Block

var x;

This example shows how to assign a value when declaring a variable:

Code Block

var x = 0;

You can also declare multiple variables by using one var statement:

Code Block

var x, y = 1, z;

Data types

...

A string is a list of characters. A string literal is indicated by enclosing characters in single quotes ' or double quotes ".

For example:

Code Block

var myString = 'JavaScript has strings.";

...

Let's say that you want to declare two lines of text in a variable:

Code Block

var myString = 'This is the first line.
This is the second line';

The way this is done will result in a syntax error, because JavaScript interprets the second line as a separate statment. The correct way to do this is by using an escape character:

Code Block

var myString = 'This is the first line.\nThis is the second line.";

...

Escape code

Value

\b

Backspace

\t

Horizontal tab

\n

Line feed (new line)

\v

Vertical tab

\f

Form feed

\r

Carriage return

\'

Single quote

\"

Double quote

 

Backslash

\OOO

Latin-1 character represented by the octal digits OOO. The valid range is 000 to 377.

\xHH

Latin-1 character represented by the hexidecimal digits HH. The valid range is 00 to FF.

\uHHHH

Unicode character represented by the hexidecimal digits HHHH.

...

Numbers are integers or floating-point numeric values:

Code Block

var myNumber = 3.14;
Boolean

Boolean data types take on one of two possible values: true or false. A boolean literal is indicated by using these values directly in the code:

Code Block

var myBoolean = true;
Undefined

An undefined data type is used for variables and object properties that do not exist or have not been assigned a value. The only value an undefined type can have is undefined:

Code Block

var x;
var x = String.noSuchProperty;

 

NULL

The NULL value indicates an empty or non-existent value, but the data type is defined. The only value a NULL data type can have is NULL:

Code Block

var x = null;
Note
titleNote

When undefined and NULL data types are being compared, the result will be true.

...

An object can hold any type of data and is the primary mechanism by wich tasks are carried out. Data contained in an object are properties for the object. Properties are accessed with the dot operator . followed by the property name:

Code Block

objectName.propertyName
Array

The array data type is an ordered set of values grouped together under a single identifier. The easiest way to create an array is to define it as a standard variable and then group the set of values in brackets:

Code Block

var myArray = [1, 5, 68, 3];

Arrays can contain data of different data types:

Code Block

var myArray = ['Bob', 5, true, x];

To create an array with no length:

Code Block

var myArray = new Array();

To create an array with a predetermined length, but no specific data:

Code Block

var myArray = new Array(4);

The elements of an array are accessed by providing an index value within brackets. In the following example variable x equals Bob and variable y equals true:

Code Block

var myArray = new Array('Bob', 5, true, x);
var x = myArray[0];
var y = myArray[2];

...

For example, first we assign a string value to the variable and after that we assign a numeric value to it:

Code Block

var x = 'Hello';
x = 5.25;

...

  • Assignment =
  • Addition +
  • Substraction or unary negation -
  • Multiplication *
  • Division /
  • Modulus %

For example:

Code Block

x = a + b;
x = a - b;
Increment/Decrement

This example shows how a is set to the value of b before b is incremented by 1.

Code Block

a = b++;

This example shows how a is set to the value of b after b is incremented by 1.

Code Block

a = ++b;

This example shows how a is set to the value of b before b is decremented by 1.

Code Block

a = b--;

This example shows how a is set to the value of b after b is decremented by 1.

Code Block

a = --b;
Logical

Here are some logical operators:

...

Here is a tertiary statement:

Code Block

x = if (test, result one, result two);

This example shows if p is greater than or is equal to 25 then x = yes and otherwise x = no.

Code Block

x = (p >= 25) ? 'yes' : 'no'

...

Expressions are the building blocks of many JavaScript statements. Any combination of variables, operators and statements which evaluate some result, like a sentence or a phrase. Literals and variables are the simplest kind of expressions and can be used to create more complex expressions.

For example:

Code Block

var x = 3 + 3;

Statements

Statements are the essence of JavaScript. They are a set of instructions to carry our specific actions. JavaScript statements may take the form of conditionals, loop or object manipulations.

One of the most common statements is an assignment. This is a statement that uses the = operator and places the value on the right-hand side into the variable on the left. Here is an example of an assignment:

Code Block

x = y + 10;
Note
titleNote

The assignment operator = should not be confused with the "is equal to" comparison operator == which is used in conditional expressions.

...

This example shows the creation of a new string object:

Code Block

var myString = new String();

...

For example, length is a property of the object string:

Code Block

myString.length

Functions and methods

...

A method is simply a function which is contained in an object and is a member function of the object. Methods of objects are accessed the same way as properties, with trailing parentheses immediately following the name of the method. If the method takes arguments (parameters), the arguments are included in the parentheses.

For example:

Code Block

application.output('Hello World.');

...

Objects are created using constructors. These are methods that create a fresh instance of an object.

For example:

Code Block

var myObject = new Object();

...

Data contained in an object are said to be properties of the object. Properties are accessed with a dot . operator (a period) followed by the property name.

Syntax:

Code Block

objectName.propertName

Functions

Functions are a special type of JavaScript object that contains executable code. A function is called (invoked) by following the function name with parentheses (). The parenthesis can contain parameters (arguments) which are pieces of data that are passed to the function when it is invoked.

Syntax:

Code Block

objectName.functionName()

...

Functions that are part (a member) of objects are know as methods.

Syntax:

Code Block

objectName.methodName

Forms

...

If the form you are referencing is the currently active form, the syntax is:

Code Block

controller.methodName

If the form you are referencing is not the currently active form, the syntax is:

Code Block

forms.formName.controller.methodName

...

If the form you are referencing is the currently active form, the syntax is:

Code Block

elementName.propertyName

If the form you are referencing is notthe currently active form, the syntax is:
Syntax:

Code Block

forms.formName.elementName.propertyName