Vaidehi Joshi Profile picture
Jul 28, 2017 15 tweets 3 min read Twitter logo Read on Twitter
In today's edition of "oh my god everything is so freaking interconnected", I learned a W I L D thing about JavaScript's parseInt function:
You're probably already be familiar with how parseInt() works; it converts a string into an integer, based on the radix/base you pass to it.
For example:

parseInt("150.00", 10)
=> 150

Pretty straightforward, right?
So what happens when you try to parseInt() something that's not a number? It ought to return NaN, as it's not a number. Except LOOK at this:
When you parseInt() with the string argument "null" and a radix of 24, it returns...a number?! 😮

WAT.
If you remember your number systems, you know that in hexadecimals, after you get to 9, you start adding letters as digits: A, B, C, D, E, F
Guess what? In base 24, the letter "n" just so happens to have a value, LOL. The "n" in "null" is recognized/converted as a numerical value!
Here's the math: in base 24, 0-9 are the first 10 digits. Then come the letters. And since "n" is the 14th letter, it has a value in base 24
Since "n" can be interpreted, it moves on to the next letter: "u". But "u" doesn't have a value in base 24, since there are only 24 digits!
So, the parseInt function just returns the integer value for what "n" represents in base 24 → 23.

Wild, right?! And also kind of amazing.
Based on this logic, I bet you could figure out which base you'd need to use in order to convert the ENTIRE "null" string into an integer.
Since "u" is the 21st letter of the alphabet and there are 10 digits (0-9) before the letters, the first radix we can covert at is: base 31!
Moral of the story: you never know when the fundamentals will come in handy! Learning about bases for #basecs made this easy for me to see.
The other moral is lol, javascript ¯\_(ツ)_/¯
PS this is what happens whenever @wycats shows me something that blows my mind—I spent 30 minutes in my JS console trying to understand it 😂

• • •

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

Keep Current with Vaidehi Joshi

Vaidehi Joshi 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 @vaidehijoshi

Aug 23, 2018
I was rejected by Etsy one year into my career. At the end of the a long day of interviews, they asked me to code a CS problem (Conway's Game of Life) in a language that I definitely didn't know (PHP). #shareyourrejections
After the interview, I went home and read everything I could about Conway's Game of Life. I didn't understand a lot of it. So then I started reading about other computer science problems, hoping one of them would make more sense to me.
But even some of that was still confusing. So I started trying to comprehend the fundamentals. How did these algorithms work, and how could I better understand them? I wasn't sure, but I knew that I had to try to find out.
Read 8 tweets
Jul 9, 2018
I stand by all the sentiments in this thread. 🙌
I've been in this industry for some years now and yet people still assume that I'm very junior because I give talks about how hexadecimals work (apparently that's "too basic" for a conference).
I get flack for writing posts about things that you might have seen in the first semester of your CS program (apparently everyone "already knows how linked lists works").
Read 8 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 on Twitter!

:(