Swapna Kumar Panda โœจ Profile picture
Jan 21 โ€ข 13 tweets โ€ข 3 min read
There may be a "Hole" in your Array

๐ŸŸช๐ŸŸช๐ŸŸช๐Ÿ•ณ๐ŸŸช๐ŸŸช

A JavaScript ๐Ÿงต
โžŠ What is a hole?

โฌ’ Holes are indices โ€œinsideโ€ an Array that have no associated element.

โฌ” An array with at least a single hole in it is called a "Sparse Array".

โฌ“ An array without any hole is called a "Dense Array".
โž‹ How to detect a hole?

Check if

โฌ™ 0 โ‰ค index < Array's length
โฌ˜ index is not in the array

Example:

To check if there is a hole at index "2" in "arr" whose length is 5

โ‡ฅ !(2 in arr)
โžŒ How to create a hole?

There are at least 4 ways.

โฌ– Omitting values in array literals
โฌ˜ Assigning a value to an index > length of array
โฌ— Increasing value of length property
โฌ™ Delete an element from existing index ImageImageImageImage
โž What value will you get from a hole?

undefined

โ‡ฅ let arr = [10,,20]
โ‡ฅ console.log(arr[1])
โžŽ How holes are handled?

They are handled in many different ways. So be cautious.

โฎ‘ Ignores holes
โฎ‘ Ignores holes, but preserves in output
โฎ‘ Preserves holes
โฎ‘ Treats holes as undefined
โฎ‘ Doesn't ignore holes
โžŽ.โžŠ Ignores Holes

โฎ‘ for...in loop
โฎ‘ Array.prototypes's
โฎ‘ forEach()
โฎ‘ indexOf()
โฎ‘ lastIndexOf()
โฎ‘ filter()
โฎ‘ every()
โฎ‘ some()
โฎ‘ reduce()
โฎ‘ reduceRight()
โžŽ.โž‹ Ignores holes, but preserves in output

โฎ‘ Array.prototype's map()
โžŽ.โžŒ Preserve Holes

Array.prototype's
โฎ‘ concat()
โฎ‘ copyWithin()
โฎ‘ push()
โฎ‘ unshift()
โฎ‘ slice()
โฎ‘ sort()
โžŽ.โž Treats holes as undefined

โฎ‘ for...of loop
โฎ‘ Array.from()
โฎ‘ Spread (...)
โฎ‘ Array.prototype's
โฎ‘ entries(), keys(), values()
โฎ‘ find(), findIndex()
โฎ‘ pop(), shift()
โฎ‘ join(), toString()*
โฎ‘ Function.prototype.apply()

* They convert undefined to ''
โžŽ.โžŽ Doesn't ignore holes

โฎ‘ Array.prototype's fill()
โž Final Words

โ‘ JavaScript allows to assign values to a non existent index which may create holes.

โ’ While handling an array with holes (sparse array), always pay attention.
With this we come to the end of this ๐Ÿงต where we discussed about "Holes in JavaScript Arrays".

If you don't want to miss such tutorial threads on HTML, CSS and, JavaScript, follow {@swapnakpanda}

Till we meet again, stay safe and stay sanitised. ๐Ÿ‘‹

โ€ข โ€ข โ€ข

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!

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

Jan 23
๐Ÿ’› CSS: border-radius

Explained with Classic Illustrations Image
๐Ÿ“‹ Table of Contents

โžŠ Introduction
โž‹ How to specify?
โž‹.โž€ One/Two/Three/Four Values
โž‹.โž Percentage Value
โž‹.โž‚ Horizontal and Vertical Radius
โžŒ Other Related Properties
โž Shapes using border-radius
โžŽ Conclusion
โžŠ Introduction Image
Read 12 tweets
Jan 22
๐Ÿ“š Books you must read for

๐Ÿ›  DSA
๐Ÿ›ข SQL
๐ŸŒ JavaScript

โ‡ฉ
โฌ– Books were and still are the most important medium for our knowledge growth.

โฌ˜ Do you love to read books? How about for technical subjects? If no, I would suggest you should.

โฌ— Here, I will list few important ones. If you want to add any, feel free.
๐Ÿ›  DSA

โžŠ Grokking Algorithms
โœ๏ธ Aditya Bhargava

โž‹ Introduction to Algorithms
โœ๏ธ Thomas H. Cormen โŠ• Charles E. Leiserson โŠ• Ronald L. Rivest โŠ• Clifford Stein

โžŒ Introduction to Algorithms
โœ๏ธ Udi Manber
Read 9 tweets
Jan 19
๐Ÿ’ซ 100 Inspiring Accounts

If their tweets appeared in your timeline, you would definitely learn something new.

They are experts in

๐Ÿ™ Web Development
๐ŸŽ‘ UI/UX
๐Ÿ‘ฉโ€๐Ÿ’ป Programming
๐Ÿ›ข Database
๐Ÿ›  System Design
๐Ÿ”ฉ Machine Learning
๐ŸŽฏ DSA
โ›“ Blockchain
๐ŸŒ Web3
Read 8 tweets
Jan 18
How is "Divide by Zero" handled in JavaScript?

๐Ÿงต
โžŠ Why is "Divide by Zero" special?

Let's say

a รท 0 = y
โ‡’ a = y ร— 0
โ‡’ a = 0

It means

โฌ” a non-zero number can never be divided by zero

โฌ• as any number multiplied by zero is zero, 0 รท 0 is also indeterminate
โž‹ How do programming languages handle it?

โ‘ As we saw above, division by zero in every scenario is indeterminate.

โ’ Many programming languages consider it a special case and throws error/exception at runtime.

โ‘ To handle, we need to catch the error.
Read 6 tweets
Jan 16
What is < onclick=" return false;" > in HTML?

โ‡ฉ
โžŠ Why is it used?

โ‘ Events automatically lead to certain actions performed by the browser.

โ’ In some cases, we may not want default action to be performed.

โ‘ "return false" triggers browser to prevent taking default action.
โž‹ Let's break it down

โœ› onclick
It's an HTML attribute.
It's being used to set inline event handlers.

โœ› =""
The code written inside "" should be valid JS code.

โœ› return false
Returning false from an inline event handler
Read 8 tweets
Jan 16
๐Ÿ’™ 10K+ Followers
๐Ÿ“… 15-JAN

1K โ‡จ First 3 months
10K โ‡จ Next 3 months

Though I have been sharing the best possible learning contents, the kind of love and support I have received from you all is beyond my words.

You will receive even better contents, that's my promise

โ†“
To never pollute my timeline, I rarely share any non-educational tweets.

Today I thought of sharing few single-liners that I wrote before but never able to post them.

Please read and share your views.
Just because a 12 yo doing Web Dev, don't mistaken it as a child's stuff.
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

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(