🔥 9️⃣ Useful Hacks to simplify your JavaScript code

Follow these simple yet most useful hacks in your code. These are not only modern way of writing JS code but also minifies your code.

🧵 👇
We will cover,

1️⃣ Converting to Boolean
2️⃣ Converting to Number
3️⃣ Converting to String
4️⃣ Short-Circuit && and ||
5️⃣ Default Function Parameters
6️⃣ Nullish Coalescing Operator
7️⃣ Optional Chaining Operator
8️⃣ Array Resizing
9️⃣ Converting Array-Like to Array
1️⃣ Converting to Boolean

To convert any data to a Boolean type, use

→ Boolean function or,
→ !! operator
2️⃣ Converting to Number

To convert any data to a Number type, use

→ + operator or,
→ Number function or,
→ parseInt function
3️⃣ Converting to String

Use String function when you need to convert any data to a String type.
4️⃣ Short-Circuit && and ||

Instead of using traditional if-condition statements, try to use short-circuit && or, ||

Use this trick only when the expression inside "if" is just a one-liner expression or a simple function.
5️⃣ Default Function Parameters

To assign missing values on function parameters, instead of using || operator, use default function parameters.
6️⃣ Nullish Coalescing Operator

Instead of manually checking whether an input is either null or, undefined, use nullish coalescing operator (??).
7️⃣ Optional Chaining Operator

To avoid TypeError, instead of checking whether a property exists in an object manually, use optional chaining operator (?.)
8️⃣ Array Resizing

To remove few last elements from an array, simply modify the array's length property value. Items from the array will automatically be removed.
9️⃣ Converting Array-Like to Array

An Array-Like object is like an Array which has a length property and, can be iterated like an array.

E.g., arguments inside function body, a string object etc.

Use Array.from() or, spread operator to convert Array-Like to an Array.
That's it guys. Did you find these hacks useful and interesting?

Reply any additional hacks you are aware of. Share me your feedbacks as well.

See you in next 🧵 soon. 👋

• • •

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

Keep Current with Swapna Kumar Panda ✨

Swapna Kumar Panda ✨ 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!


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 @swapnakpanda

12 Oct
🔥 Array and Object Destructuring in JavaScript 🚀

This 🧵 will have everything that you may need to learn this important feature of "Destructuring".

Let's explore 👇
We will discuss about

→ History (why this feature was necessary)
→ Introduction to Destructuring
→ How does destructuring work?
→ Limitations and future improvements

Let's deep dive 🚀
0️⃣ Some History

Back to 2015, JS codes were a little heavy.

If I had an array, say

arr = [10, 20]

To access elements of the array, I had to use index. Eg., arr[0], arr[1].

And if I had to store these values in some other variables, then

first = arr[0]
second = arr[1]

Read 22 tweets
17 Aug
6 Insert Techniques for JavaScript Arrays

Check the 🧵 👇

#DEVCommunity #javascript

Insert an element at the beginning of an array by both mutating and not mutating the original array.


Insert an element at the end of an array by both mutating and not mutating the original array.

Read 6 tweets
16 Aug
🔍 Search Operation Explained for Non-Programmers

A comprehensive 🧵 👇 🔍  Search Operation Explained for Non-Programmers
In this 🧵, I will explain

🙋🏼‍♀️ What is Search?
🙋🏼‍♀️ Where do we use it?
🙋🏼‍♀️ How important is it?
🙋🏼‍♀️ How better can it be?

Understand about concepts like Sorting, Hashing.

No programming knowledge required for reading this thread.

What is Search? - Search is basically when you are looking for something.

You must have come across terms like 'Find', 'Filter', 'Fetch', 'Get', 'Check', 'Select', 'Read', 'Look for' etc. They all involve 'Search'.
Read 19 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!