20 of my most popular JavaScript Tips 💡

Thread 🧵👇
Pass arguments as an object.

The meaning becomes much more clear from the names of the properties.
Also, the order doesn’t matter anymore.

Trust me - your teammates will be happy 🙌
Extending the standard built-ins is considered bad practice.

Create your own utility class instead 🙌
(...And share it on NPM, if it's useful to others).
Did you know that you can use the Broadcast Channel API to do basic communication between browser tabs, windows, and iframes?

It's pretty simple 🙌
Here's an example of how you can create a reusable and composable pipe using JavaScript.
Use console.trace instead of console.log.
It will show you the complete call stack when debugging.
Understanding closures.

A closure gives a function access to an outer function's scope, even if the inner function is invoked from a completely different context.
The nullish coalescing operator will return its right-hand operand when the left side is null or undefined. Not just falsy.

When working with numbers, this is typically very useful.
Did you know that you can cancel a fetch request using an AbortController?

A common use-case is React:
If a component unmounts while a fetch call is awaited, it can be useful to abort the call.
Instead of using find(), or manually searching a list for an occurrence, use the array method some() instead.

It’s built for exactly that purpose.
TypeScript comes with a utility type, 'ReadonlyArray<T>'.

It's equivalent to 'Array<T>', but with all mutating methods removed.

In this way, you can make sure you don’t change your arrays after creation.
Did you know that you can use object destructuring on arrays?

This is very convenient when we need a group of values from specific indexes.
Did you know that you can trigger a "scroll into view" on a specific element using a single function call in JavaScript?

You can even add a "smooth" behavior to get a nice smooth scroll animation.

Try it for yourself 😎
By using function composition, you can compose functions for different purposes.

In this case, we're using one function to create different "setter" functions for updating state in React.
Use proper variable names - also in callbacks!

Short, concise code is not necessarily an ideal.
Clarity and readability is.

Paying with an extra line is perfectly ok.
Did you know that you can use curly braces with switch-statements?

Takeaways:
🔸 More readable
🔸 Establishes their own block scope
Did you know that you can create your own custom HTML Elements using JavaScript - and then use them in your HTML file just like any other element? 😎

You can create some pretty powerful experiences using this technique.
Did you know - if you add the option -y to npm init, NPM will create a starter setup for you without having to go through all the questions 🙌
You can set timers using console.time.
This can be useful when debugging slow loops or function calls.
3 options you can pass to addEventListener.

{ once: true } can be a lifesaver on some occasions!
Avoid callback hell while using NodeJS builtins.

You don't need promisify either - from NodeJS 10 and up, you can import the promisified versions directly from '[module]/promises' 💪
Did you like these tips? 🤩

Don't forget to like/retweet this thread. Thank you!

Also, you can find all 65+ of these in my FREE e-book 👇
simonhoiberg.com/ebooks/65-code…

• • •

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

Keep Current with Simon Høiberg

Simon Høiberg 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 @SimonHoiberg

16 Sep
JavaScript ES2021 🚀
It's here!!

Are you up to speed with some of the cool new features we got in ES2021?

Let's take a look 👇🧵
JavaScript ES2021 (or ES12) - was published in June 2021 and introduces some cool new features to the JavaScript language.

Should you care?!

Well, it's not groundbreaking (like ES6), but it does introduce some features that you should familiarize yourself with.

Let's dive in!
🔸 String.prototype.replaceAll()

The current 'String.prototype.replace()' method only replaces the first occurrence, unless a regular expression with a global modifier is provided.

With the new 'String.prototype.replaceAll()' method, we can finally omit the regex 👇
Read 11 tweets
14 Sep
🔥 Functional Style JavaScript 🔥

The most popular and widely accepted style of writing JavaScript in 2021.

But what is "functional style" and why is it so popular?

Let's take a look 🧵 👇
JavaScript is a multi-paradigm programming language.

This means that the language is open for programming in different styles, including object-oriented, procedural, prototypal, and functional.

By far, the most common styles you see are object-oriented and functional.
🔹 Imperative vs. declarative

👉 Object-oriented programming follows an imperative paradigm.

👉 Functional programming follows a declarative paradigm.

Let’s look at the difference.
Read 29 tweets
31 Aug
5 annoying JavaScript habits that you want to avoid.

I see these 5 over and over.

They are bad for performance, readability, and they signal a lack of basic understanding of the JavaScript language.

Let me go through them here 🧵👇 Image
Using map() instead of forEach().

Arrays expose different methods for different purposes.
If you are simply iterating through a list, you want to use forEach(), not map().

Using the wrong method may miscommunicate the intent. Image
Creating an extra arrow function (thus an extra function closure), while not being necessary is both bad for readability and bad for performance.

It's a sign that the author doesn't truly understand that in the nature of JavaScript, you can pass functions by reference. ImageImage
Read 7 tweets
19 Aug
Looking for your dream job in tech?

Use these tips to catch the attention of the tech recruiters.

👇🧵
🔸Have a great LinkedIn profile

Your LinkedIn profile is probably the most important asset for your career.

This is typically going to be the first impression the recruiters get from you, so make sure it stands out GREAT!

Here are so tips on how to improve it 👇
🔹 Have a great profile image

Make sure to have a great profile image:
- Look professional
- Clean background
- Great lighting

You can use this service to analyze the quality of your LinkedIn image.
snappr.com/photo-analyzer/
Read 18 tweets
19 Aug
🔥 Kill this interview question 🔥

🎯 The challenge
Write a function that — given a string — returns true if the string is a palindrome or false otherwise.

Let's look at 11 different approaches to solving this in JavaScript 👇🧵
👌 The definition

A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as taco cat or madam or racecar or the number 10801.

- Wikipedia
According to the definition, we need to strip potential non-alphanumeric characters thus convert to lower-case.

So in all of the following solutions, we will be using a "clean" function to get a clean sequence of characters.
Read 39 tweets
17 Aug
Back End in 2021?
Where should you put your bets?

Let's take a look at GitHub.

Laravel ⭐ 66.2K
Django ⭐ 59K
Flask ⭐ 56.3K
Spring Boot ⭐ ️56.6K
Express.js ⭐ ️54K
Ruby on Rails ⭐️ 48.8K
Meteor ⭐️ 42.6K
Nest ⭐️ 39.5K

Now, let's talk about them 👇🧵 Image
🔸 Laravel

Laravel is a Web Framework for PHP.
Yes, that's right. PHP.

It's still one of the most popular choices out there.

github.com/laravel/laravel
🔸 Django

Django is a Web Framework based on Python.
It's an extremely popular choice for creating high-level Web Applications using Python.

github.com/django/django
Read 10 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

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

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(