Can You Guess This JavaScript Output? (Hard)

preview_player
Показать описание
Can you guess what this tricky JavaScript code is going to do and why?

Prepping for your frontend interviews? Use code "conner" for a discount on my course FrontendExpert:

Рекомендации по теме
Комментарии
Автор

This was tough lol. Did not think it would take in two parameters like this.

Inkarnid
Автор

I love your content. This is why I try to use arrow functions more often than not, because code can have unexpected outcomes otherwise.

ShiloBuff
Автор

When you call a function inside the *.map()* method:

1- The first argument is always the current array element
2- The second argument is always the index value
3- The third argument is always the entire array itself

When calling parseInt("7") by itself, the second argument defaults to 10, which is base-10 number system used for decimals. But when calling parseInt within .map(), the .map() method passes the index as the second argument to parseInt, leading to unexpected behavior, which you should avoid by wrapping it in a function like this:

['0', '1', '2', '10'].map(num => parseInt(num))

alfred.clement
Автор

This is a cool example of "working as designed, please read the docs"
The best way to do this as intended is to declare an arrow function inside the map and pass the value to the parseInt()

eduardoromaguera
Автор

On this one was tricky but makes sense. I didn't know parseInt could take two arguments. Thanks for doing this.

sanesanyo
Автор

What I would do is array.map( (item) => parseInt(item) )

sdscode
Автор

nooo you can't just give these crazy question ideas for all the interviewers out there

ninhdang
Автор

i really hate implicty function passing like this, we should always explicitly pass the arguments to the functions

rickvian
Автор

O my God

I can't believe this so if we don't pass the radix in base 10 it will give us that
Thank you for your brain 🧠
I appreciate 🙏

balogunmuhammadawwal
Автор

Damn thanks for this, why does it automatically take both of the parameters though?

dorianurem
Автор

Doesn’t this make parseInt totally misleading when used with map? How is this not a flaw in the language?

Zeegoner
Автор

I didn’t understand the base 1 thingy. I didn’t even know there could be a base 1. And why does it support from 2 through 36? Someone explain please! Haha I’m googling but I don’t understand.

jmy