Javascript/Actionscript: Lexical Structure

 

The lexical structure of a programming language is a set of basic rules that specify how you write programs in this particular language. This structure specifies the lowest level of syntax and structure used in the programming language.

The following is generally true of both Javascript and Actionscript. In the text below, when referring to Javascript, I mean BOTH Javascript and Actionscript.

 

 

Case Sensitivity

Javascript is a case-sensitive language. This means that capital letters must be used consistently. (i.e. "School" and "school" are NOT the same thing in Javascript). Note that HTML on the other hand is not case-sensitive.

Whitespaces and Line Breaks

Javascript ignores all white spaces - this includes carriage returns and the space character - between tokens. A token is defined as any word, number or other entity which obviously cannot have a white character inserted in the middle of it. This flexibility allows for a wide variety of formatting possibilities when you write Javascript.

Lines are optionally ended with a semicolon

A line is ended in Javascript with a semicolon. However, the semicolon is optional if there is a new line character placed at the end of the line where the semicolon would normally appear.

 

Comments

Comments are placed in code in order to allow the programmer to place explanations of how things work, to labels various variables and what they do, etc. Javascript supports C and C++ style comments.

Any text between a "//" and the end of the line is considered to be a comment.

Any text enclosed between "/*" and "*/" regardless of how many lines it is on is considered to be a comment.

Examples:

// In Javascript this text would be a comment.

 

/* and so would

this text */

 

Literals

A literal is a data value that appears directly in a program. These are generally either numeric values, a text string (a list of alphanumeric characters which is how computers represent text), or a Boolean logical value (a value of either true or false - these values are used to indicate "states" that various things are in, and will be covered extensively later in this course). Examples of literals are:

12

1.3

"The World at Large"

'I am also a character string'

true

false

 

Identifiers

Identifiers are the names that you use to name various objects you will create in Javascript. The most calmer uses of identifiers are to name variables - places where you can store numerical values or character strings - and functions - sub routines that the programmer can create to do specific repetitive jobs within their program.

The rules for naming identifiers are fairly simple. An identifier name must begin with a lower or uppercase letter, the underscore("_") or the dollar sign ("$"). Subsequent characters may be any of these characters, and may also be any of the digits 0-9.

Finally, and importantly, there are many reserved words which the program already has a purpose assigned to. Identifiers that you create may not be any of these keywords for the obvious reason that a great deal of confusion will result from this dual usage of the same word.

 

Reserved Words

In general, the best practice is to avoid using any word which is likely to be used by the browser. For example, the variable name "image" is a very poor choice, whereas a variable name like "myImage" is likely to be unique.

Here are some of the words reserved by Javascript:

 

abstract

boolean

break

byte

case

catch

char

class

const

continue

debugger

default

delete

do

double

else

enum

export

extends

false

 

for

final

function

if

finally

float

goto

implements

import

in

instanceof

int

interface

long

native

new

null

package

private

protected

 

public

return

short

static

super

switch

synchronized

this

throw

throws

transient

true

try

typeof

var

void

while

with