Monte Carlo compute of π (HTML5).
s.length is a property, not a function.
s.charAt(0) returns the first character.
s.charCodeAt(0) returns the unicode.
String.fromCharCode(n1, n2, ...).
s.indexOf("hello") or s.lastIndexOf("hello")
return the index of "hello" in s.
s.search(pattern) regular expression search, return the position of first match.
s.match(pattern) regular expression search, return an array of strings.
s.replace(pattern, newstring) regular expression replacement.
s.toLowerCase() and s.toUpperCase().
s.substr(start, length) if length is missing, from start to the end.
s.substring(start, end) if end is missing, from start to the end.
s.slice(start, end) like substring, negative value from end of string.
s.concat(s1, s2, ...) returns s + s1 + s2 + ...,
also conside strarr.join("").
s.split(delim, limit).
var a = new Array(),
var a = new Array(n),
var a = new Array(1, 2, 3)
or var b = ["dog", "cat", "tree"].
n = a.length; (set) or a.length = 5; (get).
function Point(x, y) {this.x = x; this.y = y;} var a = new Point(3.5, 5.3);
to add z: Point.prototype.z = null; a.z = 4.55; .
x = a.pop(): remove the last element, return this element.
a.push(x1, x2, ...): append elements.
x = a.shift(): remove the first element, return this element.
a.unshift(x1, x2, ...): add elements to the beginning.
ab = a.concat(b): return a + b.
a.slice(1,3): return the elements 2, 3 and 4.
a.splice(where, nremove, x1, x2, ...):
first remove nremove elements starting from where, then
add x1, x2, ... there. var a = [1, 2, 3, 4, 5, 6]; a.splice(2, 3, 7, 8)
→ [1, 2, 7, 8, 6].
b.join(" and ")
→ "dog and cat and tree".
a.reverse().
a.sort() or a.sort(sortfunc)
with function sortfunc(a, b) { return b - a; } (descending).
Math.PI, Math.E, Math.SQRT2
Math. prefix):
abs(x),
ceil(x),
floor(x),
round(x),
max(x1, x2, x3, ...), and
min(x1, x2, x3, ...)
sqrt(x),
pow(x,y),
exp(x),
log(x)
cos(x),
sin(Math.PI/5),
tan(x),
acos(x),
asin(x),
atan(x),
atan2(y, x)
random().
var d = Date();d.getDay() returns 0-6, 0 is Sunday.
d.getDate() returns 1-31.
d.getMonth() returns 0-11, 0 is January.
d.getFullYear() returns 2011.
d.getMilliseconds() returns 0-999.
d.getSeconds() returns 0-59.
d.getMinutes() returns 0-59.
d.getHours() returns 0-23.
d.getTime() returns milliseconds since midnight Jan 1, 1970.
x = parseFloat("123.4");
also use isNaN(x) to detect errors.
parseInt("123")
parseInt(123.4),
or Math.floor(123.4),
or the trick in the next section.
17/5 yields 3.4, to get an integer,
we can append an bitwise operation to force the result as an integer, like
(17/5) | 0, or
(17/5) >> 0.
To remove leading and trailing spaces:
function trim(s) { return s.replace('/^\s+|\s+$/g', ''); }
or to remove all spaces
s.replace('/\s+/g', '').
eval(s)eval("1+2") returns 3,
eval("document.write('abc')") writes "abc".
Javascript can be extended through some helper scripts (Javascript frameworks), such as jQuery, MooTools, and Prototype. Click here for a list from wikipedia.
The web browser Mozilla Firefox has a web developer console (Web developer/Web Console in version 6), which can output useful error messages.
The web browser Chrome has a more useful tool to visually debug a webpage. Click the configure button next to the address bar, choose Tools/Developer Tools (Shift+Ctrl+I) and/or Tools/Javascript Console(Shift+Ctrl+J).
Internet Explorer 9 and later also has a good developer tool. Start debugging will start the Javascript debugger.
The web browser Opera (free to use)
has a more useful tool to visually debug a webpage.
In a page, right click to show the pop-up menu, click Inspect Element.
A debug window will show up.
There are two parts of Javascript Language: 1. the language itself. 2. interaction with HTML elements (called document object model, dom DOM).
The language itself is specified in ECMA website.
DOM standard is in W3C. A zip for the most commonly used DOM2 is here.