What's New
Dev Tools
Site Map

JavaScript Language Introduction

This language overview is aimed at experienced developers, new to the JavaScript Language.  JavaScript's syntax is strikingly similar to Java.  However, much of the underlying behaviour is different.

The JavaScript Language is introduced under the following headings:

  1. Major Operators
  2. Variables Rules
  3. Data Types
  4. Simple Output
  5. Major Constructs
  6. Functions

1. Major Operators

Operator Description Comment
/* */


encloses multi-line comment

indicates rest of line is a comment

= assignment  
; terminates a line (optional) x = 1 or x = 1; are both acceptable in JavaScript

Note: semi-colons can be used to separate more than one statement on a line but this style of coding is inadvisable.

== checks for equality, e.g.

if (x==1)

if you use single = when trying to check for equality:

if(j=5) {statement}

then the expression evaluates to true and the expression is also executed (j is assigned 5). This is comparable to bug-prone ‘C’ behaviour


> >=

< <=

checks for inequality

greater than, gr. than or equal

less than, less than or equal

! NOT (negates a boolean expression)  
+= -= *= shorthand operator to add/subtract/multiple two numbers and assign to first x += s (is like x = x + s, doing addition or string concat. depending on whether these are numbers or strings)
++ -- quick increment (decrement) x++ is like x = x + 1


Logical and,

Logical or respectively.

(a && b) if a is false, b is not evaluated (or executed)

(a || b) if a is true, b is not evaluated (or executed)

a ? b : c Conditional operator if boolean expr. "a" is true, use expr. "b", otherwise use expr. "c"


2. Variables Rules

JavaScript is a very loosely-typed language:

investmentTypes = new Array(5)

If extra array elements are needed, JavaScript automatically adds them.

So investmentTypes[6] = "options" would lengthen the array. JavaScript provides a rich set of supporting functions for arrays such as length (# elements), sort, reverse, push, pop, etc.

function graphLoc (xloc, yloc) {
  this.xloc = xloc
  this.yloc = yloc

The "object" can then be instantiated using the "new" keyword as follows:

baseLoc = new graphLoc(24, 32)

An array of this object could be declared as follows:

plotPoints = new Array(3)
plotPoints[0] = new graphLoc(1, 2)
plotPoints[1] = new graphLoc(3, 2)
plotPoints[2] = new graphLoc(5, 2)

The third plot point could then be displayed:

document.write("xloc = " + plotPoints[2].xloc + " yloc = " + plotPoints[2].yloc)

Note: there is nothing stopping you from accidentally referring to an invalid element in the object (e.g. plotPoints[1].xlc). JavaScript simply adds the new "element" to the custom object.

3. Data Types

According to the last value assigned to a variable, it will "become" a data type (or an object).  Each data type has its own series of rules and supporting functions.  These are outlined below.






4. Simple Output Statement in JavaScript

JavaScript can output values with the write command.

var hours = 12
var pay = 20
document.write("Hours worked: " + hours + "Payrate: " + pay);

5. Major Constructs

Statements blocks are delimited with { }.  The brace brackets are optional for single level constructs (e.g. an if with no embedded construct) but it is good style to always include them.


if ((a==b) && (b<c)) {
} else {

Note: when the block delimiters{} are excluded, JavaScript assumes there is a single line in the scope of "if" or "else". If there are multiple lines before the else, an error will result.


switch (age) {
  case 1:

  case 2:


Note: if break is omitted, the code in the remaining case statement(s) is automatically executed (until a break is encountered or the switch statement is complete).


for(i=0; i<10; i++) {


while (i<10) {


with (document.form1) {

Note: when JavaScript encounters an otherwise unknown identifier inside the with, it tries to prefix the object specified by the with.

6. Functions

6.1 General

All functions are public. JavaScript does not support private vs. public functions.

6.2 Function Declaration

function product (var1, var2) {
  result = var1 * var2
  return result

As usual, variable types of the function arguments are not explicitly specified. The fact that the function returns a value only becomes "known" when the return statement within the function is executed.


... back to JavaScript Topics

Copyright 1997-2019, Woodger Computing Inc.