5 JavaScript hacks to speed up your Javascript

Code like the pros with these helpful tips

Nicholas Ortenzio
2 min readJan 3, 2014

--

1. Stop using var to declare variables.

Javascript has a thing called scope, which means that when you declare a variable in the browser, the server needs to determine which parts of your website the variable applies to. You can avoid this AJAX call by omiting the var statement. This results in the variable being "global" (ie. it works on your entire website instead of just one page)

2. Use == instead of ===

They do the same exact thing. The reason why they both exist is because Microsoft wanted to do things “their way” (tm) and now we have to support it forever.

3. Use switch statements instead of if / else

Switch statements are a relatively unknown feature of the JPL (Javascript Programming Language). They were created to make your code easier to read when you have to nest if statements.

var foo = 2;
switch (foo==1) {
case true:
doSomething();
break;
default:
switch (foo==2) {
case true:
doSomethingElse();
break;
default:
throw "foo was not found";
}
break;
}

4. Make all your functions execute Asynchronously

Normally, JavaScript code doesn’t run until the user clicks the “execute” command in the browser window. This can make your site slow and unresponsive. You can get around this by using setTimeout()

var a = 3, b = 5;function addNumbers (a, b) { 
return (a+b);
}
// this will run the code immediately
setTimeout('var c = addNumbers(a,b)', 1);
console.log(c);

5. Just have fun!

This is more of a “mind hack”. But if you aren’t enjoying yourself, you’ll never succeed in the world of JavaScript programming. There really is no “wrong” way to program Javascript. As long as you don’t see the image below then you are doing just fine.

If anyone knows how to fix this please email me.

Nicholas Ortenzio is a Javascript Programmer who got into a car accident on his way to work today

--

--