COM271, Week 7
Javascript Overview: Language Characteristics
Syllabus | Table of Pages | Assignments | References and Useful Links
This page lists a few general characteristics of JavaScript. If you are new to programming, either skip this page or read it through once, quickly, without worrying about retaining or understanding everything that you read. We will return to all of these things in greater depth later.
Order of Execution: Scripts are executed as encountered in the flow of the page. ALthough JavaScript may appear anywhere in a page, it's best to place function definitions within the <head> element.
Case Sensitivity: JavaScript is case-sensitive; myvar and MyVar are different variables in JavaScript. This applies to all aspects of the language, including keywords, operators, variable names, event handlers, and object names. All keywords are lowercase.
Conventions for naming variables: JavaScript uses a "camel-back" naming convention, in which methods or properties use mixed casing, e.g., lastModified property of the Document object, in which the M of lastModified must be uppercase. Because HTML requires lowercase element and attribute names, favor lowercase in JavaScript as well.
Whitespace: JavaScript ignores excess (i.e., more than 1) whitespace, as does HTML.
Statements: Assignment statements use the equal sign, assigning the expression of the right side to the variable of the left. All statements must end with a semicolon. More than one statement may be included on a line, each statement ending in a semicolon. Semicolons may be eliminated if statements are separated by a linebreak, but relying on this is bad practice: use the semicolon!
Blocks: Groups of more than one statement may be included in { } curly brackets; do this for the body of functions, for blocks of code within loops, etc.
Variables
Types: Variables have four qualities; they have names, types, values, and space in memory. JavaScript types include
- strings (characters; These are enclosed in single or double quotes)
strMyString="Hello World!"; This creates a string and assigns it to the variable strMyString. - numbers (integers, decimals, currency, dates)
- arrays a group of variables, all with the same name, distinguishable by numbers or names used to identify individual elements within the array.
myDataArray['first_name'] contains one element of a group of data (this element contains a first name, apparently). - Booleans (True or False logical values).
JavaScript is dynamically (=weakly) typed, meaning that type is determined when a value is assigned; also, type can change!
x='Hi!' (x is type string)
x=3.14 (x is now type number)
Composite Types: JavaScript is an object-oriented language. Among other things, this means that arrays can be multi-typed (single dimensioned variable contains numbers, strings, functions, etc.). Elements of an array may be identified by either name of number (myDataArray['first_name'} might also be referred to as myDataArray[2]; they are the same information, called by two different names). Array indices atart at 0 (zero); the first element in my myDataArray array is the element myDataArray[0].
Populating (assigning values to) an Array: Arrays may be defined like this:
var myArray = [1,4,9]
var myArray2 = ["phrases are okay", false, 3.14159, 6, "x**2"]
var myArray3 = new Array() {undefined length, empty}
var mayArray4 = new Array(5) {5-item array, empty}.
Expressions: Expressions allow arithmetic, string concatenations, etc., assigning a variable on the left side of the expression's equal sign (=) with the value evaluated on the right side. Operators include arithmetic +, -, *, /, and % (modulus) operators, and also bitwise & (and), | (or), ^ (not), ~(Exclusive OR), << (left shift) and >> (right shift) operators. Strings are concatenated using the string operator +. Relational (logical) operators include ==(identity), !=(not equal to), <, >, <= (less than or equal to), and >= (greater than or equal to); relational operators assign a value of true or false, depending on the value evaluated. Incrementing is provided by the ++ shorthand (also, --); i.e., x++ is equivalent to x = x + 1.
Flow Control: JavaScript supports conditional statements (if/else and switch/case), and loops (while, do/while, for, and for/in), with continue and break statements to modify flow control.
Functions: Functions are encapsulated code; they begin with the word function, and contain their code in curly brackets. Functions may be passed variables, and return assigned values.
I/O: The Window object's Alert method allows writing to the screen, in the form of a dialog box. The confirm() and prompt() methods are similar.
Regular Expressions: RegExp is a constructor which is used for pattern matching, often used to change (correct) strings.
Comments: JavaScript comments are contained between /* and */ pairs.