JavaScript tricks that you should never be using

Misho Tektumanidze
Sep 5, 2018 · 3 min read

For not such a long work experience with JavaScript I came across with too many weird tricks or hacks. While these tricks might seem useful or handy, I do not advice to use them in an actual project. Why? Well, if by seeing the code below, you won’t be able to understand it’s behavior right away, that’s the main reason. It’s a great chance that after a week, you just won’t recognize your code, I’m not even talking about other people, who will struggle to understand your intentions. Before we kick in, I want to clarify, that this article is ostly a compilation of many different talks, codepens and other articles combined into one. I want to specially credit Brian Leroux and his talk WTFJS at dotJS 2012 conference, who inspired me to write this article.


Using value returned from the OR operator

Let’s implement Elvis operator with “or” operator. If you don’t know what the Elvis operator is, you can read about it here.

So what’s going on there?! Well you might had not realized the fact, that logical operators return values, even more, returned value doesn’t have to be a boolean, it can be anything. So how exactly does the or operator behave? If expression to the left of the or operator is truthy, left expression is evaluated and the result is returned, otherwise expression to the right is evaluated and returned (examples below). Such weird behavior makes possible hacks like the one shown above.


Calling function with the ternary operator

Okay, first of all, I promise that this is the last logical operator in this article. Here you can read about the ternary operator.

So this one is the least weird and the most controversial trick, because many of you will probably disagree with me calling it a hack, but I decided to include this one, because you will almost certainly, come across such usage of the ternary operator.

So what’s wrong here, seems handy?! Well, the thing is that, most of us, expect ternary operator to return some kind of value, but in our case, none of called functions return any value. Things get even uglier when we want to call function in just one case.


Subtraction from the string

And for the last one, my personal favorite. Strings huh, what can possibly be weird here?! Try to come up with a simplest way to subtract a number from the string, which represents a number too (like ‘42’ or ‘-42’).

Well, some of you might have known, that ‘-’ works just fine in this case.

This one was easy, but what about addition? And yes, ‘+’ is no good here, because it has a completely different behavior in case of a string, it will just concatenate string and a number. So any guesses?

Just don’t tell me that you saw that coming.


So, that’s all for now. You have probably learned a few tricks, that will make your coworkers pull their hair out, when they see this code in the production build. This was my first article, so don’t be too hash on me I’m open for any advises and if you liked this article, hit that applause to spread the word!

Misho Tektumanidze

Written by

Web developer and a Computer Science student

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade