Simple Javascript Syntax Quick-Reference

Shorthand Javascript Conditional (If, Then Statement) condition ? true : false ex: window.location == "http://google.com" ? alert("you are at google.com!") : alert("you are not at google.com :("); Unassigning (Deleting) Javascript Variables variable = this.undefined; ex: myVar = this.undefined; if (myVar) alert("myVar is still defined!"); /* BUT! Javascript is garbage collected so this is almost always unnecessary! */ Multiple Variable Declaration in Javascript var variable1, variable2, variableN; ex: var myCat, myDog, myRat; Javascript Variable Assignment Across Multiple Lines var name = "text \ more text \ end of text"; ex: var myString = "my string \ more string \ more string"; alert(myString);

Javascript Heredoc: a Multi-line String var variable = (<r><![CDATA[ content ]]></r>).toString(); ex: var myHeredoc = (<r><![CDATA[ An unaltered multiline string! ]]></r>).toString(); alert(myHeredoc);

Javascript Array and Function Pointers, References • • Objects, arrays, and functions are all reference types Primitives (numbers, Booleans, and for all practical purposes Strings as well) are not stored as references

Function Reference (Pointer) Demonstration: var myFunc = function(a){ return a; } // test myFunc var test = myFunc(“hi”); alert(test);

// test == “hi” // “hi”

// function REFERENCE var test = myFunc; // test is a REFERENCE alert(test); // “function(a){ …” // let’s try this out alert( test(“hello”) );

// “hello”

And with Arrays: var myArray = [1, 2, 3]; var arrayRef = myArray; arrayRef[2] = “new!”; alert( myArray ); // [1, 2, “new!”]

/* we changed the original array because myArray and arrayRef both point to the same object in memory! */

Sign up to vote on this title
UsefulNot useful