Last Updated: February 25, 2016
·
1.632K
· vraa

5 nifty JavaScript tricks that you may not know

Over the years, I have seen several JavaScript techniques that are particularly clever. Here's a short list of 5 such techniques that I have been using again and again. I hope it inspires you too to rethink how you write certain piece of code.

1. A compact string comparison

Let's say you want to check if a string value is present in a set of strings. You obviously go for a if statement as below.

if(fruit === 'apple' || fruit === 'banana' || fruit === 'chikoo'){
    doMagic();
}

Here's a compact version of above code:

if({apple:1,banana:1,chikoo:1}[fruit]){
    doMagic();
}

Read rest of the article

2 Responses
Add your response

Would love to hear about the reader's tips and tricks! :)

over 1 year ago ·

Thanks, this is very helpful!!!

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

The website is part of the attack surface, not only the API behind it. Every script, iframe, and tag on a public page is code that did not go through the same review as the backend.

Security insights from Cside