A statement in JavaScript is comparable to a sentence in English – both contain complete ideas
that the reader can understand independent of the rest of the document. A sentence is terminated
with a period, but a statement in JS always ends with a semicolon.
Example: x = 5+2;
variable
A storage container for values. Once a value has been assigned to a variable, that variable’s
identifier can be used in place of it’s value anywhere in a program. A variable’s value can be
changed at any point in the program (hence the name variable). Below myname is the identifier
and "Paul" is the value.
Example: var myname = “Paul”;
object
A part of the web page or browser that JavaScript can control. Primary objects in JavaScript
include:
window | The browser window – has control over things like the status bar and scroll bars. |
document | The contents of the web page – has control over things like text color and size. |
An object can contain other objects as properties.
property
All objects have a unique set of properties accessible to the programmer. Changing one or more
of these properties will change the way an object looks or behaves. In the following example,
window is the object and status is the property.
Example: window.status = “Hi there”;
Other examples of objects and properties:
Object | Property |
document.bgColor | Changing this changes the background color of the web page |
window.location | Changing this loads a different page into the browser. |
method
All objects also have a unique set of methods available to the programmer. Each method will
invoke some sort of action on the object. Methods are like functions (see the function handout) in
that you pass them parameters and they perform some action with those parameters, but they are
not user-defined like functions are. Rather, methods are built into the JavaScript language.
Example: window.alert(“This is an alert message”);
Other examples of objects and methods:
Object | Property |
window.homt() | This makes the browser load its user-defined homepage. |
document.write(“bla”) | This writes the text “bla” onto the webpage. |
event handler
Many objects also have event handlers, which catch user triggered events (such as mouse clicks
and keystrokes) and allow the programmer to specify an action for each of these events.
Example: onClick= “alert(‘you clicked a button’);”
A few event handlers:
onLoad | Performs specified actions when page is loaded |
onClick | Performs specified actions when mouse is clicked on the object that contains the handler statement. |
onKeyPress | Does something when a key is pressed on the keyboard. |