Java Script
Java Script
What is JavaScript?
JavaScript is a high-level, dynamic, untyped, single
threaded and interpreted programming language
CIS 233
CIS 233
Make it Understandable
Choose easy to understand and short names for
variables and functions.
Bad variable names: x1 fe2 xbqne
Also bad variable names:
createNewMemberIfAgeOverTwentyOneAndMoonIsFull
Avoid Globals
You run the danger of your code being
overwritten by any other JavaScript added to
the page after yours
Bad
var current = null;
function init(){ };
CIS 233
Avoid Globals
You run the danger of your code being overwritten
by any other JavaScript added to the page after
yours
GOOD
var MyNamespace = {
current:null,
init:function(){
},
};
CIS 233
Avoid Globals
Always Declare Local Variables
All variables used in a function should be
declared as local variables.
Local variables must be declared with
the var keyword, otherwise they will become
global variables.
CIS 233
Declarations on Top
Give cleaner code
Provide a single place to look for local
variables
Make it easier to avoid unwanted (implied)
global variables
Reduce the possibility of unwanted redeclarations
CIS 233
Declarations on Top
// Declare at the beginning
var firstName, lastName, price, discount, fullPrice;
// Use later
firstName = "John";
lastName = "Doe";
price = 19.90;
discount = 0.10;
fullPrice = price * 100 / discount;
CIS 233
Declarations on Top
This also goes for loop variables:
// Declare at the beginning
var i;
// Use later
for (i = 0; i < 5; i++) {
CIS 233
Initialize Variables
Give cleaner code
Provide a single place to initialize variables
Avoid undefined values
CIS 233
Initialize Variables
// Declare and initiate at the beginning
var firstName = "",
lastName = "",
price = 0,
discount = 0,
fullPrice = 0,
myArray = [],
myObject = {};
CIS 233
JavaScript Terminology.
JavaScript programming uses specialized
terminology.
Understanding JavaScript terms is
fundamental to understanding the script.
Objects, Properties, Methods, Events, Functions,
Values, Variables, Expressions, Operators.
CIS 233
CIS 233
Bad Code:
var i;
for (i = 0; i < arr.length; i++) {
Better Code:
var i;
var l = arr.length;
for (i = 0; i < l; i++) {
CIS 233
Properties
Properties are object attributes.
Object properties are defined by using the
object's name, a period, and the property
name.
e.g., background color is expressed by:
document.bgcolor .
document is the object.
bgcolor is the property.
CIS 233
Methods
CIS 233
Events
CIS 233
Functions
Functions are named statements that performs
tasks.
e.g., function doWhatever () {statement here}
The curly braces contain the statements of the
function.
CIS 233
Values
Values are bits of information.
Values types and some examples include:
Number: 1, 2, 3, etc.
String: characters enclosed in quotes.
Boolean: true or false.
Object: image, form
Function: validate, doWhatever
CIS 233
Variables
Variables contain values and use the equal
sign to specify their value.
Variables are created by declaration using
the var command with or without an initial
value state.
e.g. var month;
e.g. var month = April;
CIS 233
Expressions
Expressions are commands that assign
values to variables.
Expressions always use an assignment
operator, such as the equals sign.
e.g., var month = May; is an expression.
CIS 233
Operators
Operators are used to handle variables.
Types of operators with examples:
Arithmetic operators, such as plus.
Comparisons operators, such as equals.
Logical operators, such as and.
Control operators, such as if.
Assignment and String operators.
CIS 233
CIS 233
CIS 233
CIS 233
CIS 233
CIS 233
Return to
CIS 233