7 JavaScript tips to make you feel smarter

and make other people hate you


1. Invert logic, always

Lets start off with an easy bit of optimization. The goal here is to make the most simple operations seem complex.

if (x && y) { … } // bad
if (!(!x || !y)) { … } // good

2. Use extended unicode characters in your variable names

Compiled software is essentially a black-box once it’s released to production. The same cannot be said for JavaScript. If someone wants to peak at your JavaScript code, they just need to open the browsers console and add some break points to see the state of your objects.

Stymie their progress by using non literal characters for the names of object properties

 var foo = function (person) {
// stuff happens

// perhaps a breakpoint is added here

// or they attempt to log the object
console.log(person);
}
 var person = {};
 person[‘\t’] = ‘Nicholas’;
person[‘\b’] = ‘Male’;
person[‘\r’] = ‘Programmer’;
person[‘\f’] = ‘Lover’;

Here’s what you get when you try to watch the variable

The dropdown is open but not properties are listed

And here is what happens when you try to log to the console

Use this same trick to incorporate Zalgo Text into your code


3. Brush up on your Trigonometry

Before I dropped out of college, my teachers always talked about how math and programming where closely related. In my experience I’ve found that not to be the case. In fact, I’m starting to think they were just lying to get students to enroll in more classes. Well, it’s time to put that crippling student loan debt to good use.

Instead of saying

if (!val) { … }

use

Math.floor(.5 + ((Math.cos(val)*.5)))  

The only time this will ever evaluate to true is if val is an exact multiple of 2pi. You don’t even need to worry if val isn’t a number. It really doesn’t matter. In fact, nothing matters anymore.


4. Take advantage of how forgiving JavaScript is

How many times have you accidentally used the assignment operator instead of the equality operator in an if statement? It’s incredibly annoying because it doesn’t throw an error and just causes your program to behave in unexpected.

function foo (x) {
if (x=true) {
// no matter what value is
// passed in for x, this
// will always execute
}
}
foo(false);

Someone looking at your code will see that and automatically assume it’s a mistake on your part. But we don’t make mistakes, so that person should be punished. “Fixing” it will have undesired consequences.


5. Work in non-decimal bases

Initializing a number in base-8 is deceptively similar to base-10; Just use ‘0' as the first digit.

var i = 27 // 27
var j = 027 // 23

Your co-workers might accuse you of just trying to be an asshole but you can insist that octal math is faster because all bits are naturally stored in groups of 8.


6. Whitespace doesn’t matter; Except when it does.

Everybody knows that whitespace and semicolons in JavaScript are purely optional, right? WRONG. Don’t take anything for granted.

(function () { 
var a=1,
b=2,
c=3
d=4,
e=5,
f=6;
}());
console.log(d,e,f); // 4,5,6

In the above example, we “missed” a comma. If this code had all been on one line, we would’ve gotten an error. But since it’s not, the compiler appends a semicolon after c=3. This causes d,e,f to be created as global variables. Now go ahead and use those variables everywhere, including separate files.

Again, if someone notices this and tries to fix it, it could potentially ruin completely unrelated sections of code, and instead of dealing with that mess, they’ll most likely just revert the change, proving that you are smarter.


7. Be creative

Programming is all about creativity and creativity is all about ripping off other people. Don’t be afraid to steal code and ideas, or accuse other people of stealing from you. For instance, did you know that jQuery completely ripped off of Prototype? It’s true.


Nicholas Ortenzio practices saying the alphabet backwards, just in case.