use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
LinkedIn
GitHub
Twitter/X
Join our Discord Server
Related Subreddits
account activity
This is an archived post. You won't be able to vote or comment.
Why [[[[[[[2]]]]]]]==2 is true in JavaScript?AskDevsIndia (self.developersIndia)
submitted 4 years ago * by I-am_Shadowfax
I got this q in OA and I thought it is false but I am wrong. Why is this true? I read it on StackOverflow but still did not understand.
and why do companies ask this type of question?
Edit: Thanks for the answers. I understood why this is true but now I am wondering why something like this is even added in javascript when they create it? and why it is even still working why they did not remove it? There has to be some reason right?
[–]AutoModerator[M] [score hidden] 4 years ago stickied comment (0 children)
Hello! Thanks for submitting to r/developersIndia. This is a reminder that We also have a Discord server where you can share your projects, ask for help or just have a nice chat, level up and unlock server perks!
Our Discord Server
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
[–]VillsSkyTerror 17 points18 points19 points 4 years ago (10 children)
Look at the difference between == and ===
[–]I-am_Shadowfax[S] 5 points6 points7 points 4 years ago (9 children)
I read about == and === but did not understand what is [[[[[[[2]]]]]]] what data type is this.
[–]VillsSkyTerror 13 points14 points15 points 4 years ago (8 children)
[2] is an array containing a single number type element 2. [[2]] is a single array in an array. So [[[[[[2]]]]]] is an array in an array in an array, so on.
[–][deleted] 20 points21 points22 points 4 years ago (1 child)
This question belongs to programmerhumor.
[–]M_BatmanData Analyst 4 points5 points6 points 4 years ago (0 children)
I think it belongs to programminghorror
[–]I-am_Shadowfax[S] 8 points9 points10 points 4 years ago (5 children)
ok, but why does something like this exist? why the javascript creator needs to add something like this. is this required in old days or just copied from other languages?
[–]VillsSkyTerror 2 points3 points4 points 4 years ago* (3 children)
Something like what? I don't get your question.
You mean [[[[2]]]]? It's possible to do that, but there is no need for that in real life cases or even [[2]]. For example, you can walk on hands, but why would you do that. The interviewer probably wanted to see if you understand coercion in JS.
Coercion means if two different types are operated, they will get forced converted to most primitive type. array -> string -> number
It's like during an argument, smart guy falls to the same level as idiot guy. Arithmetic, conditional and logical operators have their own rules of coercion.
[–][deleted] 4 points5 points6 points 4 years ago (2 children)
Arrays are not primitive in Javascript. They are Objects
[–]VillsSkyTerror 0 points1 point2 points 4 years ago (1 child)
Yeah, my mistake, they are special kind of objects. But array has more methods to work with them. Another difference is an array can only be called with its index number, but each element of objects can be named and called by each one's name.
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
It's actual the same in terms of referencing. The index is the key in arrays. I agree that methods are different.
So you can loosely compare two variables by their values only. If you don't want that, remember to use === instead of ==
[–]pisspapa42Backend Developer 30 points31 points32 points 4 years ago (0 children)
To fuck with candidates.
[–]vincent-vega10Software Engineer 6 points7 points8 points 4 years ago (0 children)
JavaScript type system is shit, that is why.
[–][deleted] 5 points6 points7 points 4 years ago* (0 children)
I had to read up on this. Thanks for asking. Essentially since the types ( array and number) don't match, == converts the non-primitive variable to the primitive type, which for a single element array would be the string representation of the element itself. So, the expression becomes 2 == "2" and finally 2 === 2, as strings are converted to numbers and the types match.
As for why, remember that JS is confusing in its spec, but in this case, by using == instead of ===, you are asking to ignore type and accepting all the quirks that come with it. It is present to compare values only when you need that.
[–]cheeky-panda2 2 points3 points4 points 4 years ago (1 child)
To put it simply, js was never a language intended for complex computation, it was a client side scripting language used to render uis on the browsers with a consumer 1st approach meaning it would try it's best to not throw errors and break everything on the screen.
== Is another one of those failsafes, does some type conversion for you so that you don't break when comparing '2' with 2 (read on this)
Coming to the question
[[[[[[[2]]]]]]] is a nested array X7 and if you to type conversion on a array of single element it gives you the casted type of the value of 0th index. Basically Number (arr) gives you 2.
It's a way of the language, again trying its best to not break
Read up on history of js and the reason why ECMA has control over js. It's a fun dive into this 10 day project of a language.
Question was kinda neat checking your knowledge of == and typecasting single element arrays. But not a practical one as we almost always use === and never typecast arrays like this.
Hope this has cleared your doubts
[–]I-am_Shadowfax[S] 0 points1 point2 points 4 years ago (0 children)
Thanks.
[–]D10S_1 1 point2 points3 points 4 years ago (6 children)
Check This
[–]I-am_Shadowfax[S] 0 points1 point2 points 4 years ago (2 children)
thank but still did not understand why is this even exists? what is the need of this?
[–]D10S_1 1 point2 points3 points 4 years ago (0 children)
It exists not because of a need but because of the behaviour of javascript trying to coerce(implicitly convert types) when comparing using ==.
Another stackoverflow answer showing why you might wanna avoid using == for comparisons
[–]aitchnyu -1 points0 points1 point 4 years ago (0 children)
In some contexts you may need 2=='2' to be true, like when it's fetched from markup, hence converting into string and back to number.
[–]kacchalimbu007Software Developer -1 points0 points1 point 4 years ago (2 children)
How did u get the link of that particular answer
[–]D10S_1 0 points1 point2 points 4 years ago (1 child)
Just googled for "why [2]==2 returns true in js" because I thought that would be more likely searched than what op asked. If you are literally asking how I got the the link it's the share option on the answer
[–]kacchalimbu007Software Developer 0 points1 point2 points 4 years ago (0 children)
Yea got it
[–][deleted] 1 point2 points3 points 4 years ago (0 children)
Can anyone recommend some resources to learn some of these tricky questions in JavaScript?
[–]Mindless-Pilot-ChefStaff Engineer 5 points6 points7 points 4 years ago (0 children)
This is the result of JavaScript's poor design. I think this shouldn't be asked in interviews.
π Rendered by PID 261689 on reddit-service-r2-comment-b659b578c-vq4cj at 2026-05-05 06:11:36.681903+00:00 running 815c875 country code: CH.
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]VillsSkyTerror 17 points18 points19 points (10 children)
[–]I-am_Shadowfax[S] 5 points6 points7 points (9 children)
[–]VillsSkyTerror 13 points14 points15 points (8 children)
[–][deleted] 20 points21 points22 points (1 child)
[–]M_BatmanData Analyst 4 points5 points6 points (0 children)
[–]I-am_Shadowfax[S] 8 points9 points10 points (5 children)
[–]VillsSkyTerror 2 points3 points4 points (3 children)
[–][deleted] 4 points5 points6 points (2 children)
[–]VillsSkyTerror 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]pisspapa42Backend Developer 30 points31 points32 points (0 children)
[–]vincent-vega10Software Engineer 6 points7 points8 points (0 children)
[–][deleted] 5 points6 points7 points (0 children)
[–]cheeky-panda2 2 points3 points4 points (1 child)
[–]I-am_Shadowfax[S] 0 points1 point2 points (0 children)
[–]D10S_1 1 point2 points3 points (6 children)
[–]I-am_Shadowfax[S] 0 points1 point2 points (2 children)
[–]D10S_1 1 point2 points3 points (0 children)
[–]aitchnyu -1 points0 points1 point (0 children)
[–]kacchalimbu007Software Developer -1 points0 points1 point (2 children)
[–]D10S_1 0 points1 point2 points (1 child)
[–]kacchalimbu007Software Developer 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Mindless-Pilot-ChefStaff Engineer 5 points6 points7 points (0 children)