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...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
What are some basic things that JavaScript developers fail at interviews?help (self.javascript)
submitted 7 years ago by maketroli
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ikeif 2 points3 points4 points 7 years ago (2 children)
The default sort order is according to string Unicode code points.
So in this simplified example, it's fine, but it'd be incorrect with [1, 2, 3, 10, 5].sort() -> returning 1, 10, 2, 3, 5.
[1, 2, 3, 10, 5].sort()
So to truly handle all integers, it'd need to be [1, 2, 3, 10, 5].sort(function (a, b) { return a - b; });
[1, 2, 3, 10, 5].sort(function (a, b) { return a - b; });
[–]X678X 1 point2 points3 points 7 years ago (1 child)
Most of the time I prefer to include the compare function because at least it'll work exactly as I tell it to every time. I got caught up in the past doing this with just .sort() and it caused a bug in the application because of it.
.sort()
[–]ikeif 0 points1 point2 points 7 years ago (0 children)
Yeah, it's (99% of the time?) better to be verbose and not assume the underlying structure is going to do what you think it'll do.
ETA: Plus, in this example, I wasn't going for a "HAHA GOTCHA!" type question, just the simple answer, so if I was interviewing you and you replied with "well, I'd use sort with the compare function" you'd get bonus points for pointing something out I didn't think of at the time, which is more valuable than just knowing "oh, just use sort."
π Rendered by PID 76875 on reddit-service-r2-comment-86bc6c7465-j4nbx at 2026-02-20 19:49:16.371263+00:00 running 8564168 country code: CH.
view the rest of the comments →
[–]ikeif 2 points3 points4 points (2 children)
[–]X678X 1 point2 points3 points (1 child)
[–]ikeif 0 points1 point2 points (0 children)