Harsh Makadia Profile picture
Dec 16, 2021 20 tweets 7 min read Read on X
16 JavaScript best practices to improve your code quality

THREAD 🧵:
Before we proceed,

"Programs must be written for people to read, and only incidentally for machines to execute."

—Unknown
{ 1 } Optional Chaining

The optional chaining operator (?.) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid. Optional Chaining
{ 2 } Data Structure

Carefully consider how you choose your data structures, Access on the left snippet here is ~70% faster than the one on the right because the left one requires no iteration. The right does. Faster Access
{ 3 } Add Dynamic Object Properties

While defining an object, there might be a situation where you need to define the object's properties dynamically.

Let’s look at the example below. Dynamic property
{ 4 } Ternary Operators

The conditional (ternary) operator takes three operands:

1. A condition followed by a question mark ( ? )

2. An expression to execute if the condition is truthy followed by a colon ( : )

3. The expression to execute if the condition is falsy
{ 5 } Destructuring Assignment

This is a clear blessing to keep the code well-formatted. Unpacking objects or arrays is easier than ever before. Destruction
{ 6 } let vs const

Use const even for updating the array, it works as you are not reassigning anything.

Don't use let over const for the array. todolist
{ 7 } Use Template Literals

Strings that we create with double or single quotes have a lot of limitations.

You might want to replace some of your strings with template literals to make working with them a lot easier.
{ 8 } Naming conventions

Take a look are below the examples

JavaScript variables are case-sensitive. Therefore, JavaScript variables with lowercase and uppercase characters are different
Naming Convention Continued ...
{ 9 } findIndex

This is the most underrated feature when it comes to finding a property value from an array of an object.

Use this over normal for loops.
{ 10 } Write Your Own Javascript Contracts and Docstrings

// Adding comments inside a function or using Docstring in the code makes things easier to understand.

Learn more here: jsdoc.app/index.html
{ 11 } Arrow functions

Arrow functions are another very important feature introduced recently to JavaScript. They come bearing many advantages.
{ 12 } Null-ish coalescing

Before introducing the null-ish coalescing operator, we had to use the OR operator || to fall back to a default value if the input was absent.

null coalescing operator ??, offers a better alternative
{ 13 } Make use of shorthand notation(Object Literals)

When designing objects or arrays in JavaScript, line space can be saved by opting for shorthand notation.

This is accomplished by setting the properties or cells of an object or array during declaration rather than after.
{ 14 } Don’t use delete to remove an item from an array

Use splice instead of using delete to delete an item from an array.

Using delete replaces the item with undefined instead of removing it from the array.
{ 15 } Set default values

When creating objects, you can set default values for some or all properties of the object.

Doing so ensures the values of each attribute are not undefined.
{ 16 } Avoid the use of eval() or the Function constructor

Use of eval or the Function constructor are expensive operations as each time they are called script engine must convert source code to executable code.
Thanks for checking this out.

If you liked this thread,

1. Follow @MakadiaHarsh for more such thread for wen development, no-code and personal growth.

2. Retweet the first tweet to help others.

Cheers! 🥂

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Harsh Makadia

Harsh Makadia Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @MakadiaHarsh

Jun 7, 2024
Meet Rolf Dobelli.

For the last 15 years, he has been a world-renowned expert in decision-making.

I've read 100 hours of content by him to improve my judgment.

7 razors to think more clearly (and become a better entrepreneur): Image
1. Action Bias

When you're always doing something, you neglect thinking.

Ask yourself:

• Is it worth it?
• Is this the best way to do it?
• Am I trying to control things outside my control?

This aligns my long-term goals with my short-term goals.
2. Fundamental Attribution Error

People underestimate external factors.

You sometimes fail to recognize that results are still out of your control, no matter how hard you work. As people think, "I've given my best, but it didn't work out."

This leads to quitting.
Read 9 tweets
Apr 21, 2024
One of the best books on decision-making:

The Essentialist by Greg McKeown.

After reading it, I stopped being busy and started being impactful.

7 principles from him to make smart decisions in your life: Image
1. The essentialist

Essentialism is not about how to get more things done.

It's about doing things less but better. It's a method of choosing between good things and a few really great things.

It's a way to think, eliminate, and execute your priorities.
2. Rule of 90%

The way you choose your options is under your control.

One way to do so is to adopt the 90% rule. Start by prioritizing important tasks. Then give each task a score b/w 0-100. Any task less than 90 (even 89) is a 0.

If it isn't a clear yes, then it's a clear no.
Read 10 tweets
Jan 16, 2024
AI will eat 85 Million jobs by 2025.

But it will also create 97 Million new jobs.

7 skills you can learn to stay ahead of the curve:
1. AI content creators

We're already seeing the rise of AI content creation tools.

In the next decade, we'll need more creators who can use AI to deliver quality content at a much quicker speed. So, keep playing around with those tools.

You'll see it pays off.
2. Integration Specialist.

AI is removing anything that is low effort.

Companies are continuously looking at their systems to find what can be replaced by AI. Now, this process needs expert supervision to find those parts.

You can be the expert.
Read 11 tweets
Jul 24, 2023
Say goodbye to time-consuming email writing!

With ChatGPT, you can craft irresistible emails in under 5 minutes.

Here are 7 Prompts so good you cannot miss : Image
1. Generate the email subject line.

Write an eye-catching email subject line using ChatGPT.

• Replace it with any topic you want.

• And you will get 10 different types of names & catchphrases.
Prompt:

“Write 10 attention-grabbing email subject lines on the topic “Lead Generation.” Use your humor or curiosity to make it more compelling.” Image
Read 18 tweets
Jul 22, 2023
Video Editors are DEAD ☠️

AI for content creators is taking the world by storm.

Here's how Submagic is going to revolutionize short-form content:

Thread 🧵: Image
1. Go to and click on "Create my video now" https://t.co/jRjgZhBwblsubmagic.co
Image
Step 2. Upload the video you want the caption to be added

Example Video:
Read 9 tweets
Jul 8, 2023
Gmail has 1,800,000,000+ active users.

But only a few use the platform effectively.

Here are 10 productivity hacks you'll regret not knowing ( until now ) :
1/ Create infinite aliases

Use the "+" ( plus sign ) to create unlimited aliases.

If your email address is johndoe@gmail.com
You can use:
For work -> johndoe+work@gmail.com

For Newsletters -> johndoe+newsletters@gmail.com

Every Email will get delivered to your inbox.
Read 13 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(