Skip to content

Instantly share code, notes, and snippets.

@nola
nola / assoicativearray.js
Created November 26, 2013 01:36
The old school way of setting up an array was to create a named array and then add each named element one by one. A quicker and more readable way is to add the elements at the same time using the object literal notation. Please note that Associative Array are essentially JavaScript Objects with properties.
var skillSet = new Array();
skillSet['Document language'] = 'HTML5';
skillSet['Styling language'] = 'CSS3';
skillSet['Javascript library'] = 'jQuery';
skillSet['Other'] = 'Usability and accessibility';
//short hand version
var skillSet = {
'Document language' : 'HTML5',
'Styling language' : 'CSS3',
@nola
nola / variableShorthand.js
Created November 26, 2013 01:38
Declaring variables Shorthand
var x;
var y;
var z = 3;
//shorthand
var x, y, z=3;
@nola
nola / operators.js
Created November 26, 2013 01:40
JS operators shorthand
x=x+1;
minusCount = minusCount - 1;
y=y*10;
//shorthand version
x++;
minusCount --;
y*=10;
@nola
nola / ifshorthand.js
Created November 26, 2013 01:43
If Shorthand
if (likeJavaScript == true)
//shorthand
if (likeJavaScript)
//another version
var a;
if ( a != true ) {
// do something...
}
@nola
nola / functionArgs.js
Created November 26, 2013 01:45
Function Variable Arguments Shorthand
function myFunction( myString, myNumber, myObject, myArray, myBoolean ) {
// do something...
}
myFunction( "String", 1, [], {}, true );
//shorthand
function myFunction() {
console.log( arguments.length ); // Returns 5
for ( i = 0; i < arguments.length; i++ ) {
@nola
nola / foreachloop.js
Created November 26, 2013 01:53
JavaScript foreach Loop Shorthand
for (var i = 0; i < allImgs.length; i++)
//shorthand
for(var i in allImgs)
//with array
function logArrayElements(element, index, array) {
console.log("a[" + index + "] = " + element);
}
[2, 5, 9].forEach(logArrayElements);
@nola
nola / charat.js
Created November 26, 2013 01:54
charAt() Shorthand
"myString".charAt(0);
//shorthand
"myString"[0]; // Returns 'm'
@nola
nola / switchshorthand.js
Created November 26, 2013 02:04
switch shorthand
switch (something) {
case 1:
doX();
break;
case 2:
doY();
break;
@nola
nola / negativewhile.js
Created November 26, 2013 02:10
Decimal base exponents You can use 1 and 0 to represent true and false. I’ve seen this used in JavaScript game development in shorthand while loops. Note that if you use the negative start your array may be in reverse. You can also use while(i++<10) and you don't have to add the i++ later on inside the while.
var i=0;
while (i<9)
{
//do stuff
i++; //say
}
//shorthand
var i=9;
@nola
nola / gist:7652479
Created November 26, 2013 02:12
Shorter IF’z If you have multiple IF variable value comparisons you can simply ass them to an array and check for presence. You could use $.inArray as an alternative.
if( myvar==1 || myvar==5 || myvar==7 || myvar==22 ) alert('yeah')
//shorthand
if([1,5,7,22].indexOf(myvar)!=-1) alert('yeah baby!')