// ============= // = FUNCTIONS = // ============= function(argument1,argument2){return argument1 + argument1;}; function (argument1, argument2) {return argument1 + argument1;}; function funkName(argument1, argument2) { return argument1 + argument1; }; function funkName(argument1, argument2) { return argument1 + argument1; }; function(argument1, argument2){ return argument1 + argument1; }; function(argument1, argument2) { return argument1 + argument1; }; new Function("argument1", "argument2", "return argument1 + argument1;"); var funkName=new Function("argument1", "argument2", "return argument1 + argument1;"); var funkName1 = function funkName (argument1, argument2) {return argument1 + argument1;}; var funkName1=function funkName(argument1,argument2){return argument1 + argument1;}; function myFunc(argument1, argument2){} Sound.prototype = {} Sound.prototype.play = function(){} Sound.prototype.play = myfunc Sound.play = function(){} ({ foobar: function(){}, 'foo': function(){}, "foo": function(){} }) // ================== // = Function Calls = // ================== funkName(parameter1, parameter2) objectName.methodName(parameter1, parameter2) objectName.objectName.methodName(parameter1, parameter2) funkName(funkName(parameter1, parameter2), parameter2) funkName(parameter1, funkName(parameter1, parameter2)) objectName.methodName(objectName.methodName(parameter1, parameter2), parameter2) objectName.methodName(parameter1,objectName.methodName(parameter1, parameter2)) objectName.objectName.methodName(objectName.objectName.methodName(parameter1, parameter2), parameter2) objectName.objectName.methodName(parameter1,objectName.objectName.methodName(parameter1, parameter2)) // ========================== // = Variables / Properties = // ========================== variableName variableName == "stringName" variableName = "stringName" objectName.5 // SYNTAX ERROR objectName().5 // SYNTAX ERROR objectName .5 // SYNTAX ERROR objectName() .5 // SYNTAX ERROR /** #!/usr/bin/env ruby code = <<-JS */ a +variableName ; // Arithmetic Operator a -variableName ; // Arithmetic Operator a *variableName ; // Arithmetic Operator a /variableName ; // Arithmetic Operator a %variableName ; // Arithmetic Operator a ++variableName ; // Arithmetic Operator a --variableName ; // Arithmetic Operator a -variableName ; // Arithmetic Operator Unary a =variableName ; // Assignment Operator a +=variableName ; // Assignment Operator a -=variableName ; // Assignment Operator a *=variableName ; // Assignment Operator a /=variableName ; // Assignment Operator a >>=variableName ; // Assignment Operator a <<=variableName ; // Assignment Operator a >>>=variableName ; // Assignment Operator a &=variableName ; // Assignment Operator a |=variableName ; // Assignment Operator a ^=variableName ; // Assignment Operator variableName+a ; // Arithmetic Operator variableName-a ; // Arithmetic Operator variableName*a ; // Arithmetic Operator variableName/a ; // Arithmetic Operator variableName%a ; // Arithmetic Operator variableName++ ; // Arithmetic Operator variableName-- ; // Arithmetic Operator variableName-a ; // Arithmetic Operator Unary variableName=a ; // Assignment Operator variableName+=a ; // Assignment Operator variableName-=a ; // Assignment Operator variableName*=a ; // Assignment Operator variableName/=a ; // Assignment Operator variableName>>=a ; // Assignment Operator variableName<<=a ; // Assignment Operator variableName>>>=a ; // Assignment Operator variableName&=a ; // Assignment Operator variableName|=a ; // Assignment Operator variableName^=a ; // Assignment Operator a &variableName ; // Bitwise Operator a |variableName ; // Bitwise Operator a ^variableName ; // Bitwise Operator ~variableName ; // Bitwise Operator a <>variableName ; // Bitwise Operator a >>>variableName ; // Bitwise Operator variableName&a ; // Bitwise Operator variableName|a ; // Bitwise Operator variableName^a ; // Bitwise Operator variableName<>a ; // Bitwise Operator variableName>>>a ; // Bitwise Operator a ==variableName ; // Comparison Operator a !=variableName ; // Comparison Operator a ===variableName ; // Comparison Operator a !==variableName ; // Comparison Operator a >variableName ; // Comparison Operator a >=variableName ; // Comparison Operator a a ; // Comparison Operator variableName>=a ; // Comparison Operator variableName 5; x >= 5; x < 5; x <= 5; x = x +5; // is the same as x += 5; x = x -5; // is the same as x -= 5; x = x+5; // is the same as x += 5; x = x-5; // is the same as x -= 5; x = x*5; // is the same as x *= 5; x = x/5; // is the same as x /= 5; x = x%5; // is the same as x %= 5; x += 5 x -= 5 x *= 5 x /= 5 x %= 5 x ++ x -- x = x&5; // is the same as x &= 5; x = x|5; // is the same as x |= 5; x = x^5; // is the same as x ^= 5; x = x<<5; // is the same as x <<= 5; x = x>>5; // is the same as x >>= 5; x = x>>>5; // is the same as x >>>= 5; x &= 5 x |= 5 x ^= 5 x <<= 5 x >>= 5 x >>>= 5 // =========== // = Strings = // =========== var stringName = ""; var stringName = ''; var stringName0 = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; var stringName1 = "Lorem ipsum dolor sit amet, consectetur adipisicing e" + "lit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." + " Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nis" + "i ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehen" + "derit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. E" + "xcepteur sint occaecat cupidatat non proident, sunt in culpa qui offici" + "a deserunt mollit anim id est laborum."; // Allow escaped newline var stringName2 = "Lorem ipsum dolor sit amet, consectetur adipisicing e\ SHOULD be scoped as string" var stringName2 = 'Lorem ipsum dolor sit amet, consectetur adipisicing e\ SHOULD be scoped as string' // First newline should be scoped as constant.character.escape.newline.js // End string at unescaped newline // SYNTAX ERROR: unterminated string literal var stringName3 = "Lorem ipsum dolor sit amet, consectetur adipisicing e Should NOT be scoped as string" var stringName3 = 'Lorem ipsum dolor sit amet, consectetur adipisicing e Should NOT be scoped as string' // First newline SHOULD be scoped as invalid var stringName4 = "Lorem ipsum dolor sit amet"; // "" SHOULD be scoped as string & invalid.deprecated var stringName5 = "Lorem ipsum dolor sit amet // "" Should NOT be scoped as string // WARNING: regular expressions should be preceded by a left parenthesis, assignment, colon, or comma {unquotedString: null} {'singleQuotedString': null}// SYNTAX ERROR: invalid label {"doubleQuotedString": null}// SYNTAX ERROR: invalid label ({'singleQuotedString': null}) ({"doubleQuotedString": null}) switch (something){ case "stringName" : break; case unquotedString : break; case /regex/ : break; case 999.999 : break; case funkName() : break; default : // do something } switch(expression){case expression:statements;break;default:statements;} while(condition){statements;} do{statements;}while(condition); for(initialization;condition;update){statements;} for(variable in object){statements;} for(variable in object)if(filter){statements;} try{statements;}catch(variable){statements;}finally{statements;} eval("expressions") Function("expressions") setTimeout("expressions",999) setInterval("expressions",999) for (var i=0, j=9; i <= 9; i++, j--) document.writeln("a["+i+"]["+j+"]= " + a[i][j]); // ======================= // = Getters and Setters = // ======================= // {get prop [ funname ]() { . . . } } var o = { get latest () { if (this.log.length > 0) { return this.log[this.log.length - 1]; } else { return null; } }, log: [] } delete o.latest; // {set prop [ funname ](val) { . . . } var o = { set current (str) { return this.log[this.log.length] = str; }, log: [] } delete o.current; // =============== // = in Operator = // =============== // Arrays var trees = new Array("redwood", "bay", "cedar", "oak", "maple"); 0 in trees // returns true 3 in trees // returns true 6 in trees // returns false "bay" in trees // returns false (you must specify the index number, // not the value at that index) "length" in trees // returns true (length is an Array property) // Predefined objects "PI" in Math // returns true // Custom objects var mycar = {make: "Honda", model: "Accord", year: 1998}; "make" in mycar // returns true "model" in mycar // returns true var color1 = new String("green"); "length" in color1 // returns true var color2 = "coral"; "length" in color2 // generates an error (color is not a String object) var mycar = {make: "Honda", model: "Accord", year: 1998}; delete mycar.make; "make" in mycar; // returns false var trees = new Array("redwood", "bay", "cedar", "oak", "maple"); delete trees[3]; 3 in trees; // returns false var mycar = {make: "Honda", model: "Accord", year: 1998}; mycar.make = undefined; "make" in mycar; // returns true var trees = new Array("redwood", "bay", "cedar", "oak", "maple"); trees[3] = undefined; 3 in trees; // returns true // ============== // = instanceof = // ============== var isInstance = objectName instanceof objectType; var color1 = new String("green"); color1 instanceof String; // returns true var color2 = "coral"; color2 instanceof String; // returns false (color2 is not a String object) var theDay = new Date(1995, 11, 17); // Dec. 17, 1995 if (theDay instanceof Date) { print("theDay is a Date object"); // whatever else... } var myString = new String(); var myDate = new Date(); myString instanceof String; // returns true myString instanceof Object; // returns true myString instanceof Date; // returns false myDate instanceof Date; // returns true myDate instanceof Object; // returns true myDate instanceof String; // returns false function Car(make, model, year) { this.make = make; this.model = model; this.year = year; } var mycar = new Car("Honda", "Accord", 1998); var a = mycar instanceof Car; // returns true var b = mycar instanceof Object; // returns true // ======= // = new = // ======= // objectName = new objectType(param1 [,param2] ...[,paramN]) function Car() {} car1 = new Car() alert(car1.color) // undefined Car.prototype.color = null alert(car1.color) // null car1.color = "black" alert(car1.color) // black function car(make, model, year) { this.make = make; this.model = model; this.year = year; } mycar = new car("Eagle", "Talon TSi", 1993); kenscar = new car("Nissan", "300ZX", 1992); function person(name, age, sex) { this.name = name; this.age = age; this.sex = sex; } rand = new person("Rand McNally", 33, "M"); ken = new person("Ken Jones", 39, "M"); function car(make, model, year, owner) { this.make = make; this.model = model; this.year = year; this.owner = owner; } car1 = new car("Eagle", "Talon TSi", 1993, rand); car2 = new car("Nissan", "300ZX", 1992, ken); car2.owner.name // ============= // = Operators = // ============= /* Precedence Operator type Associativity Proximity */ //Individual operators ; /* 1 member left-to-right both */ a . b ; /* 1 member left-to-right inside + left */ a [ b ] ; /* 1 new right-to-left right */ new b ; /* 2 function-call left-to-right inside + left */ a ( b ) ; /* 3 decrement n/a right-or-left */ -- b ; /* 3 decrement n/a right-or-left */ a -- ; /* 3 increment n/a right-or-left */ ++ b ; /* 3 increment n/a right-or-left */ a ++ ; /* 4 bitwise-not right-to-left right */ ~ b ; /* 4 delete right-to-left right */ delete b ; /* 4 logical-not right-to-left right */ ! b ; /* 4 typeof right-to-left right */ typeof b ; /* 4 unary-+ right-to-left right */ + b ; /* 4 unary-negation right-to-left right */ - b ; /* 4 void right-to-left right */ void b ; /* 5 division left-to-right both */ a / b ; /* 5 modulus left-to-right both */ a % b ; /* 5 multiplication left-to-right both */ a * b ; /* 6 addition left-to-right both */ a + b ; /* 6 subtraction left-to-right both */ a - b ; /* 7 bitwise-shift left-to-right both */ a << b ; /* 7 bitwise-shift left-to-right both */ a >> b ; /* 7 bitwise-shift left-to-right both */ a >>> b ; /* 8 in left-to-right both */ a in b ; /* 8 instanceof left-to-right both */ a instanceof b ; /* 8 relational left-to-right both */ a < b ; /* 8 relational left-to-right both */ a > b ; /* 8 relational left-to-right both */ a <= b ; /* 8 relational left-to-right both */ a >= b ; /* 9 equality left-to-right both */ a != b ; /* 9 equality left-to-right both */ a == b ; /* 9 equality left-to-right both */ a !== b ; /* 9 equality left-to-right both */ a === b ; /* 10 bitwise-and left-to-right both */ a & b ; /* 11 bitwise-xor left-to-right both */ a ^ b ; /* 12 bitwise-or left-to-right both */ a | b ; /* 13 logical-and left-to-right both */ a && b ; /* 14 logical-or left-to-right both */ a || b ; /* 15 conditional right-to-left both */ a ? b : c ; /* 16 assignment right-to-left both */ a = b ; /* 16 assignment right-to-left both */ a %= b ; /* 16 assignment right-to-left both */ a &= b ; /* 16 assignment right-to-left both */ a *= b ; /* 16 assignment right-to-left both */ a += b ; /* 16 assignment right-to-left both */ a -= b ; /* 16 assignment right-to-left both */ a /= b ; /* 16 assignment right-to-left both */ a ^= b ; /* 16 assignment right-to-left both */ a |= b ; /* 16 assignment right-to-left both */ a <<= b ; /* 16 assignment right-to-left both */ a >>= b ; /* 16 assignment right-to-left both */ a >>>= b ; /* 17 comma left-to-right both */ a , b ; // ============== // = Deprecated = // ============== // The escape and unescape functions are deprecated. Use encodeURI, encodeURIComponent, decodeURI or decodeURIComponent to encode and decode escape sequences for special characters. escape() unescape() encodeURI() encodeURIComponent() decodeURI() decodeURIComponent() // ================== // = Reserved Words = // ================== // The following are reserved words and may not be used as variables, functions, methods, or object identifiers. // The following are reserved as existing keywords by the ECMAScript specification: break case catch continue default delete do else finally for function if in instanceof new return switch this throw try typeof var void while with // The following are reserved as future keywords by the ECMAScript specification: abstract boolean byte char class const debugger double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile // Take note that while not specified in ECMA-262, const, export, and import have been implemented in Mozilla. const export import // Labels var objectName = { labelName: "", labelName2: "" } labelName: while (i < 4) { document.write(i + "
"); i += 1; labelName2: while (j > 4) { document.write(j + "
"); j -= 1; if ((j % 2) == 0) continue labelName2; document.write(j + " is odd.
"); } document.write("i = " + i + "
"); document.write("j = " + j + "
"); } // Unorganized new new Function(); new (function(){})(); new(function(){})(); // Comments /**/ /* */ /** **/ // /* Block Comment */ /** Documentation Comment (idiom) **/ // Inline Comment // ========== // = Blocks = // ========== /* Comment Block */ "stringiness"; /* String */ 'stringiness'; /* String */ /regex/; /* Regex */ function myFunction(param1, param2){}; /* parameters */ myFunction=function(param1, param2){}; /* parameters */ myFunction(arg1, arg2); /* arguments */ label: 'label': "label": for(initialization;condition;update){ statements; } for(variable in object){ statements; } [1, 2, 3]; /* Array */ var myObj = { label:'value' }; /* Object Block */ var myFunction = function(){ statements; }; /* Function Block */ myFunction(arg1, arg2); /* arguments on multiple lines */ // =========== // = Invalid = // =========== /* Precedence Operator type Associativity Proximity */ //Individual operators ; /* 1 member left-to-right both */ a . b ; /* 1 member left-to-right inside + left */ a [ b ] ; /* 1 new right-to-left right */ new b ; /* 2 function-call left-to-right inside + left */ a ( b ) ; /* 3 decrement n/a right-or-left */ -- b ; /* 3 decrement n/a right-or-left */ a -- ; /* 3 increment n/a right-or-left */ ++ b ; /* 3 increment n/a right-or-left */ a ++ ; /* 4 bitwise-not right-to-left right */ ~ b ; /* 4 delete right-to-left right */ delete b ; /* 4 logical-not right-to-left right */ ! b ; /* 4 typeof right-to-left right */ typeof b ; /* 4 unary-+ right-to-left right */ + b ; /* 4 unary-negation right-to-left right */ - b ; /* 4 void right-to-left right */ void b ; /* 5 division left-to-right both */ a / b ; /* 5 modulus left-to-right both */ a % b ; /* 5 multiplication left-to-right both */ a * b ; /* 6 addition left-to-right both */ a + b ; /* 6 subtraction left-to-right both */ a - b ; /* 7 bitwise-shift left-to-right both */ a << b ; /* 7 bitwise-shift left-to-right both */ a >> b ; /* 7 bitwise-shift left-to-right both */ a >>> b ; /* 8 in left-to-right both */ a in b ; /* 8 instanceof left-to-right both */ a instanceof b ; /* 8 relational left-to-right both */ a < b ; /* 8 relational left-to-right both */ a > b ; /* 8 relational left-to-right both */ a <= b ; /* 8 relational left-to-right both */ a >= b ; /* 9 equality left-to-right both */ a != b ; /* 9 equality left-to-right both */ a == b ; /* 9 equality left-to-right both */ a !== b ; /* 9 equality left-to-right both */ a === b ; /* 10 bitwise-and left-to-right both */ a & b ; /* 11 bitwise-xor left-to-right both */ a ^ b ; /* 12 bitwise-or left-to-right both */ a | b ; /* 13 logical-and left-to-right both */ a && b ; /* 14 logical-or left-to-right both */ a || b ; /* 15 conditional right-to-left both */ a ? b : c ; /* 16 assignment right-to-left both */ a = b ; /* 16 assignment right-to-left both */ a %= b ; /* 16 assignment right-to-left both */ a &= b ; /* 16 assignment right-to-left both */ a *= b ; /* 16 assignment right-to-left both */ a += b ; /* 16 assignment right-to-left both */ a -= b ; /* 16 assignment right-to-left both */ a /= b ; /* 16 assignment right-to-left both */ a ^= b ; /* 16 assignment right-to-left both */ a |= b ; /* 16 assignment right-to-left both */ a <<= b ; /* 16 assignment right-to-left both */ a >>= b ; /* 16 assignment right-to-left both */ a >>>= b ; /* 17 comma left-to-right both */ a , b ; // SYNTAX ERROR myVar myVar myVar$ myVar myVar_ myVar myVar0 myVar () myVar () _myVar () $myVar 00 () A0 () //ok () , //ok () ; //ok () /*operator*/ //ok myVar /*both operator*/ myVar //valid () /*both operator*/ () //valid /*right operator*/ myVar //valid /*right operator*/ () //valid