<script language="JavaScript"> ... </script>
<script language="JavaScript">
alert("Hello world!");
</script>
<script language="JavaScript" src="jscode.js">
<a href="javascript:alert('Error');">
<hr width="&{barWidth};%" >
x = 42;
var x;
var answer = 42; // an integer ... answer = "hello"; // ... is now a string
answer = "hello" + 42; // hello42 "37" - 7 // 30 "37" + 7 // 37
parseInt("37") + 7 // 44 (1st way)
("37" - 0) + 7 // 44 (2nd way)
switch (action) {
case "recalculate":
recalculate();
break;
...
default:
statement;
}
function square(number) {
return number * number;
}
arguments[i] // index starts at 0
function concat(separator) {
result = "";
for(var i = 1; i<arguments.length; i++) {
result += arguments[i] + separator;
}
return result;
}
var weekdays = new Array("Sun", "Mon", "Tue",
"Wed", "Thu", "Fri");
var mon = weekdays[1];
weekdays.length
var today = new Date(); var dayOfWeek = weekdays[today.getDay()];
var max = Math.max(x, y);
var phone = "613-234-6789";
var i = phone.indexOf("-");
var area = phone.substring(0, i);
var rest = phone.substring(i, phone.length);
var phoneParts = phone.split(/-/);
var car = new Object(); car.make = "Pontiac"; car.model = "GrandAm"; car.year = 2000;
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
var car = new Car("Pontiac", "GrandAm", 2000);
car.make
car.model = "Taurus"; car.color = "red";
car["make"] // Pontiac car["model"] // GrandAm car["color"] // red car["year"] // 2000
for(var prop in car) {
document.write("car." + prop + "= " +
car[prop] +"<br>");
}
objectName = {property1: value1,
propertyN: valueN};
var car = {make: "Pontiac", model: "GrandAm",
color: "red", year: 2000};
object.methodName = functionName;
function total() {
return (this.qty * this.unitPrice);
}
function LineItem(name, qty, price) {
this.name = name;
this.qty = qty;
this.unitPrice = price;
this.total = total; // method
}
var lineItem =
new LineItem("Perl and CGI", 2, 28.95);
document.write("You pay $" +
lineItem.total());
function makeItAJaguar(theObject) {
theObject.make = "Jaguar";
}
makeItAJaguar(car);