Most STUPID Javascript tricks you shouldn't use

The Internet is great for developers, but sometimes, you come about stupid articles like "amazing JS tips you NEVER knew about". Get ready for stupid Javascript examples.

Most STUPID Javascript tricks you shouldn't use

Convert string to number using '+'

Cast a string to number using + operator

This is a soft one, but let me ask you a question: what's actually preventing you from using Number() ? If you can't give me a valid answer - other than 'it's faster to type +' - I'll beat you so hard your body's gonna look like that + motherf*cker. I never want to see that in a code review.

 

Convert number to string by adding an empty string

Convert number to string by adding an empty string

This the same as above but in reverse. If I ever catch you doing this nonsense instead of using .toString(), you better be a fast runner because I will keep chasing you until you fix this sh*tty code. Again, I never want to see that in a code review.

 

Using delete to remove an item from an array

Using delete in array

This is just wrong. Using delete in an array just creates an empty spot and doesn't affect its length. Knowing this, using delete makes absolutely no sense. Better use the .splice function or the .filter function if you want to remove every item matching what you are searching for.

 

Useless switch statements

Useless Switch Statements

We have to talk about this. 

I see more and more people praising switch statements for completely useless cases, like the screenshot above. For simple condition checks just use if/else or ternary operator, and for bigger checks you can use a map object. Check these examples:

With ternary operator (small checks):

const bool = true
bool ? action1() : action2()

 

With a map object (large checks)

const value = 1
const action1 = () => console.log("action1")
const action2 = () => console.log("action2")
const action3 = () => console.log("action3")
const action4 = () => console.log("action4")
const action5 = () => console.log("action5")

const mapObject = {
 1: action1,
 2: action2,
 3: action3,
 4: action4,
 5: action5,
}

mapObject[value]()
// action1

I'm not denying switch statement's utility, but a lot of people - especially young developers - use them excessively for no particular reason, making the code longer than it should be.

 

Conclusion

Not everything you see on the Internet is relevant, even if the title says "Amazing tricks" or "Tricks every developer should know".  Don't forget that a lot of people write these articles just for SEO, reputation or money. They don't care to write high-quality or original content.

Remember one thing: the simpler the better. 

This doesn't mean the shorter the better, when you hesitate between a shorter and a longer code, always choose the most understandable one. Your goal is not to masturbate on your own code, but to make it optimized, simple and understandable (for you and others).

On this note, take care of yourself and don't write shitty code.