Javascript (notes)

1 of 8

Javascript is not Java. They’re really totally different languages. The similar names are a confusion perpetuated by marketing. The standard for the language established by ECMA (the European Computer Manufacturers Association) calls the language ECMAScript.

Javascript was created by Brendan Eich at Netscape in 1995 for the purpose of allowing code in webpages. It was soon incorporated into the other major browsers, and it is today the only language available in all current browsers by default.

In the browser, Javascript code sees page content as what’s called the DOM (Document Object Model); by manipulating the DOM, Javascript code can manipulate the page. To request data from a server, Javascript can use a technique called AJAX (Asynchronous Javascript and XML); before browsers added the features to make AJAX possible, the only way to get new data from a server was to navigate to a new page, which is suboptimal in many ways.

Like Pigeon, Javascript is a dynamic language and is often considered somewhat of a functional language, though it’s mainly imperative. Object-oriented programming in Javascript is facilitated by a feature called prototyping.

The data types of Javascript include: numbers, booleans, strings, functions, the special value null, arrays (what Javascript calls lists), and objects (what Javascript calls dictionaries).

2 of 8

Unlike Pigeon (but like most every other language), Javascript uses infix notation, and arithmetic is done with the traditional symbols for operators:

  • +             addition
  • -              subtraction/negation
  • /              division
  • *             multiplication
  • %            modulus

A function call in Javascript is written with the syntax:

func (arguments)

.. where func is an expression returning a function and where arguments is a list of expressions separated by commas.

Assignment in Javascript is done with the = operator, whose left operator is not an expression but rather an ‘lvalue’, a target of assignment. Because the = operator returns the value assigned and is right-to-left associative, we can chain assignments together:

x = y = z = 3      // x = (y = (z = 3))

3 of 8

Assignment statements and expression statements in Javascript end in semi-colons:

x = 3;
foo(4);

The syntax of Javascript is free-form, meaning wherever the language allows or requires whitespace, you can put any mix of whitespace characters.

End-of-line comments are written starting with //

Multi-line comments are written starting /* and ending */. You can’t nest a multi-line comment in another.

Identifiers in Javascript may consist of alphanumeric characters, dollar signs $, and underscores _, but identifiers cannot begin with a numeral.

A variable in Javascript must be declared with a var statement:

var foo;            // declare foo
var bar = 4;        // declare bar with an initial value 4

When not given an initial value, a variable starts with the special value “undefined” (which is not the same as null).

A function in Javascript is created with the reserved word function:

function(parameters) {body}

This is not a statement but rather an expression which returns a function object:

var george = function() { bla(); } ;        // assign george a new function

The logic and relational operators in Javascript are written with symbols rather than words:

  • !              not
  • ===        equals
  • !==         not equals
  • &&         and
  • ||           or
  • <             less than
  • >             greater than
  • <=           less than or equal
  • >=           greater than or equal

All values in Javascript are considered to the have the truth value “true” except:

  • false   (obviously)
  • null
  • 0
  • 0.0
  • “”   (an empty string)
  • the value “undefined”

(There actually isn’t a reserved word for the undefined value.)

4 of 8

if/else is written:

if (condition) {body}
else if (condition) {body}
else {body}

Similarly, while is written:

while (condition) {body}

Global variables (variables declared outside functions) must be declared before they are used. Local variables, in contrast, can be declared anywhere in the function, though convention is to simply put all declarations at the top of the function. If you fail to declare a local variable but still use its name, the name is assumed to refer to a global, not a local.

Functions can be written inside other functions. Functions within functions effectively form a hierarchy of namespaces for local variables: if function A is inside function B, A can see the locals of B, but B cannot see the locals of A. When names of locals conflict, the innermost name takes precedence such that outer names are not visible to the interior function. For instance, if function A is inside B and both A and B have a variable foo, foo in A always refers to the foo of A, not the foo of B; effectively, we have no way in A to refer to the foo of B.

5 of 8

A function created within another sees the variables of the call to the function in which it is created, and it even retains these variables even after the outer function has returned. This feature is called closure. A function object retains links to the local variables of the enclosing function call(s) in which the function was created.

What Javascript calls an object is what in Pigeon we called a dictionary. The keys of Javascript objects, however, can only be strings. An object is created with a literal notation:

{properties}

…where properties is a list of key-value pairs separated by commas, each of the form:

key”: value

To create, modify, or access the values of an object, use the [] operator:

object[key]

…where object is an expression evaluating into an object and key is an expression evaluating into a string.

When the characters of a key string qualify as a valid identifier, you can use these alternate syntaxes:

key: value 
object.key

When a function is invoked via an object.key expression, the object is passed into the function where it is known by the special name this. For example, when invoking foo.bar(), the object of foo is passed into the function as this. The name this cannot be reassigned another value.

This special mechanism is really just a convenience for enabling the object-oriented style of programming.

6 of 8

An object can be linked to another such that, when looking up a property in an object, if a property of that name is not found, Javascript will continue the search in the other object linked from that object. When assigning to an object, though, you are always creating/modifying a property in that object itself.

Strings are linked to an object containing methods for manipulating strings.

7 of 8

Instead of lists, Javascript has what it calls arrays, which are created with the literal syntax:

[items]

…where items is a list of expressions separated by commas. To access items in the array:

array[index]

…where index is a number.

An array is really just an object, so it really contains key-value pairs where the keys are strings; so x[3] is actually short hand for x[“3”]. However, an array is distinct from regular objects because but it has a special property length which holds the number that is one greater than the largest numeric key, e.g. if an object has a key “46” as its highest numeric value key, the key “length” should have the value 47.

Within a function, the arguments to a function are accessible in an array named arguments. When invoking a function, you can provide as many arguments as you want, so if you have more arguments than parameters, the extra arguments are only accessible in arguments. If you provide fewer arguments than the function has parameters, the parameters with missing arguments will have the value undefined.

8 of 8

An exception is an object representing an error, something that has gone wrong. Exceptions are thrown by bad operations (e.g. 3 + true) or by a throw statement:

throw expression;

When thrown, an exception propogates up the call chain until eventually propogating out of the top-level of code. At any point in the chain, however, an exception can be “caught” by try-catch:

try {body} catch (variable) {body}

When an exception occurs in the body of try, execution jumps to the body of catch with the exception passed to variable. When caught, an exception stops propogating.

Comments are closed.