Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
1. Type conversion: Number & String:
2. Min-Max Conversions: 
3. Deal with WebSocket Timeout: 
4. Avoid using try & catch: 
5. Use the Switch/case statement: 
Last Updated: Mar 27, 2024

5 Useful JavaScript Tips and Tricks

0 upvote
Create a resume that lands you SDE interviews at MAANG
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM


Javascript is one of the most popular and widely used programming languages in the world right now. It’s also the first language developers try to master. With a deep knowledge of Javascript, you can work as a front-end developer, a back-end developer, or if you’re really good at it, then a full stack developer as well. You will learn about the 5 best JavaScript tips and tricks.

For a long time, Javascript was only used to build interactive web pages. It was referred by the developers as a toy language (hence the term “Vanilla JavaScript”). But those days are long gone now. With the help of investments made by large companies and a massive community of developers, Javascript has managed to be the most preferable language across the world. Now, along with the use to display a simple alert on the web page, JS is even capable of controlling a robot.

If you’re working on JavaScript, here are some JavaScript tips and tricks that will make your life easier:


1. Type conversion: Number & String:


 Arithmetic operations like +, -, /, *, are the most common things we use within Javascript development. So it demands the operands be numbered. If by any chance the operand is not a number, JavaScript turns it into a number. There is always a possibility that you may not get an expected result, especially while operating with “+”. This happens because “+” is also utilised in the form of the concatenation of two or more strings. In such a scenario, knowing how to convert the types will give you extreme flexibility over your code, and hence your end product.

2. Min-Max Conversions


When you’re trying to deal with bounded numbers, the Math.min() and Math.max() are very convenient. By using the following example of Math.min() & Math.max() for the previous bounded value() you can understand it better:

3. Deal with WebSocket Timeout: 


After 30 seconds of inactivity of WebSocket connection establishment, a server could time out. And, also the firewall connection may show time out. You can always send a faux message to the server systematically to avoid this situation. Alternatively, you could use one code to keep alive your connection and one code to cancel the keep alive. The timeout can be shunned by these cool tricks.

4. Avoid using try & catch: 


Every time the catch clause is executed whenever the object is assigned to a variable. Dodge the try & catch as it constructs a new variable at the current scope of runtime.

Use this cool trick

Instead of this

5. Use the Switch/case statement: 


When there are 2 or more than 2 cases, it’s efficient to use Switch/case than if/else statement. It surely runs faster and it is also more sophisticated code. Avoid the same in case if the number is more than 10.


With this, you’re now ready to pace up your progress with JavaScript as a development tool. Also, if anywhere along the line you feel stuck, don’t hesitate in reaching out to us. We, at Coding Ninjas, offer courses on Web Development, Node.JS, and many other important domains. Starting from ground zero, we take you onwards and upwards!

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Previous article
What is Nightwatch.js framework?
Next article
15 Best JavaScript Books For Beginners and Experts [Updated 2023]
Live masterclass