Values, Variables, Literals and Characters
Category | Description of Syntax | Example |
values | numbers, logical, strings, null, undefined | 3.14, true/false, "Hello" |
type conversion |
number + string returns string string - number returns number |
x = "Size " + 42 // returns "Size 42" y = 12 + " eggs" // returns "12 eggs" "37" - 7 // returns 30 "37" + 7 // returns 377 |
assignment | assign a value to a variable | x=42 |
declaration | use keyword var (to prevent runtime error) | var x=42 |
conversion to numbers |
parseInt(str) parseFloat(str) |
var age = parseInt(f.age.value) var mass = parseFloat(f.mass.value) |
integer literals | digits, sign, hexadecimal | 42, -345, 0xFFF |
floating point literals |
decimal point, scientific notation | 3.1415, -3.1E12, .1e12, 2E-12 |
string literals | enclose strings in quotes (single or double) | "blah", 'blah', "1234", "one line \n another line" |
array literal | list of array values enclosed in brackets | coffees = ["French Roast", "Columbian", "Kona"] |
special or escaped characters |
\ followed by the character: backspace, formfeed, newline, carriage return, tab, single quote, double quote, backslash |
\b, \f, \n, \r, \t, \', \", \\ |
Operators
Category | Description of Syntax | Example |
assignment operators |
= to assign a value to a variable +=, -=, *=, /=, etc. are shorthand |
x = y // assigns y to x x += y // same as x = x + y |
comparison operators |
equal, not equal, greater than, less than, ge, le | x == y, x != y, x > y, x < y, x >= y, x <= y |
arithmetic operators |
addition, subtraction, multiplication, division, increment, decrement, unary negation, modulus |
1 + 2, 1 - 2, 1 * 2, 1 / 2, x++, y--, -x, x % y |
logical operators | and, or, not | false && true // returns false false || true // returns true !true // returns false |
string operator |
+ joins strings | "my" + "string" // returns "mystring" |
conditional operator |
condition ? val1 : val2 | status = (age >= 18) ? "adult" : "minor" |
Statements
Category | Description of Syntax | Example |
comments | begin one-line comments with // use /* and */ for multiple-line comments |
// This is a single-line comment. /* This is a multiple-line comment. It can be of any length, and can contain anything. */ |
conditional if..else |
if (condition) { statements1 }[else { statements2 }] | if (name == "John") { alert("Your name is John"); } else { alert("You are not John"); } |
conditional switch |
switch (expression){ case label : statement; break; case label : statement; break; ... default : statement;} | switch (gender) { case "m": title="Sir"; break; case "f": title="Madam"; break; default: title="Sir or Madam";} |
for loop | for ([initialExpression]; [condition]; [incrementExpression]) { statements} | fact = 1; for (i=2; i<=n; i++) fact *= i; |
do..while | do { statement} while (condition) | do { i+=1; document.write(i);} while (i<5); |
while | while (condition) { statements} | while (true) { alert("Hello") } // infinite loop |
Functions
Category | Description of Syntax | Example |
defining | function name (arg1, arg2, ..., argn) { statements } | function square (number) { return number * number; } |
calling | name(arg1, arg2, ..., argn) | square(5) // returns 25 |
arguments | functionName.arguments[i] or arguments[i] returns argument
i arguments.length returns number of arguments |
function myConcat(separator) { result=""; for (var i=1; i<arguments.length; i++) { result += arguments[i] + separator } return result; } |
Objects
Category | Description of Syntax | Example |
constructor | objectName = new objectType ( param1 [,param2] ...[,paramN] ) | myobject = new Car ("red", "fast", "sporty") |
destructor | delete objectName | delete myobject |
current object | this[.propertyName] | <INPUT TYPE="button" VALUE="Do something to the form" onClick="myFunction(this.form)"> |
for..in | for (variable in object) { statements } | for (var i in obj) { result += obj_name + "." + i + " = " + obj[i] + "" } |
with | with (object){ statements} | var a, x, y; var r=10; with (Math) { a = PI * r * r; x = r * cos(PI); y = r * sin(PI/2);} |
Array object |
arrayObjectName = new Array(arrayLength) or Array(element0, element1, ..., elementN) property: length methods: concat, join, pop, push, reverse, shift, slice, splice, sort, unshift |
myArray = new Array("Wind","Rain","Fire"); myArray[0] // returns "Wind" myArray.pop() // removes and returns "Fire" myArray.shift() // removes and returns "Wind" myArray.length // returns 1 |
Math object |
property: PI methods: abs, sin, cos, tan, asin, acos, atan, exp, log, ceil, floor, min, max, pow, round, sqrt |
Math.PI is 3.141592...; Math.abs(-1) is 1; Math.cos(0) is 1; Math.log(1) is 0; Math.floor(3.1) is 3; Math.min(4,5) is 4; Math.pow(2,3) is 8; Math.sqrt(16) is 4 |
String object |
property: length HTML methods: anchor, link, big, bold, etc. other methods: concat, indexOf, lastIndexOf, split, splice, substr, toLowerCase, toUpperCase |
s1 = new String("Hello") //creates a String object s1.length // returns 5 s1.bold() // returns <b>Hello</b> s1.lastIndexOf("l") // returns 3 s1.toUpperCase() // returns "HELLO" |
Date object | object for displaying date and time | today = new Date(); |