Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, and Victoria Kirst. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
body
(example)<script type="text/javascript"> JavaScript code </script>
head
or body
document.write
document.write("message");
body
document.write
typeof
functiontypeof(value)
function foo() { alert("Hello"); }
var a = ["Huey", "Dewey", "Louie"];
true
:
typeof(3.14) === "number"
typeof("hello") === "string"
typeof(true) === "boolean"
typeof(foo) === "function"
typeof(a) === "object"
typeof(null) === "object"
typeof(undefined) === "undefined"
arguments
arrayfunction example() { for (var i = 0; i < arguments.length; i++) { alert(arguments[i]); } }
example("how", "are", "you"); // alerts 3 times
arguments
representing the parameters passedfor (var name in arrayOrObject) { do something with arrayOrObject[name]; }
var map = []; map[42] = "the answer"; map[3.14] = "pi"; map["champ"] = "suns";
Map
collection or a hash table data structurevar today = new Date(); // today var midterm = new Date(2007, 4, 4); // May 4, 2007
getYear
returns a 2-digit year; use getFullYear
insteadgetDay
returns day of week from 0 (Sun) through 6 (Sat)getDate
returns day of month from 1 to (# of days in month)Date
stores month from 0-11 (not from 1-12)eval
(evil?) functioneval("JavaScript code");
eval("var x = 7; x++; alert(x / 2);"); // alerts 4
eval
treats a String as JavaScript code and runs that code