You are on page 1of 1

Simple Javascript Syntax Quick-Reference

Shorthand Javascript Conditional (If, Then Statement) Unassigning (Deleting) Javascript Variables

condition ? true : false variable = this.undefined;

ex: ex:
window.location == "http://google.com" ? myVar = this.undefined;
alert("you are at google.com!")
: alert("you are not at google.com :("); if (myVar) alert("myVar is still defined!");

/* BUT! Javascript is garbage collected so


this is almost always unnecessary! */

Multiple Variable Declaration in Javascript Javascript Variable Assignment Across Multiple Lines

var variable1, variable2, variableN; var name = "text \


more text \
ex: end of text";
var myCat, myDog, myRat;
ex:
var myString = "my string \
more string \
more string";

alert(myString);

Javascript Heredoc: a Multi-line String Javascript Array and Function Pointers, References

var variable = (<r><![CDATA[ • Objects, arrays, and functions are all reference types
content • Primitives (numbers, Booleans, and for all practical
]]></r>).toString(); purposes Strings as well) are not stored as
references
ex:
var myHeredoc = (<r><![CDATA[ Function Reference (Pointer) Demonstration:
An unaltered var myFunc = function(a){ return a; }
multiline string!
]]></r>).toString(); // test myFunc
var test = myFunc(“hi”); // test == “hi”
alert(myHeredoc); alert(test); // “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! */