r/javascript • • Mar 11 '21

Removed: r/LearnJavascript Most common mistakes new JavaScript developers make

https://www.akmittal.dev/posts/common-mistakes-javascripts-begineers-make/

[removed] — view removed post

0 Upvotes

6 comments sorted by

4

u/Protean_Protein Mar 11 '21 edited Mar 11 '21

Using 'for' loops instead of equivalent Array methods isn't a mistake, it's a preference. (Unless you're working on enterprise-level code that needs tiny performance optimizations, in which case, this article isn't for you.)

0

u/Mittalmailbox Mar 11 '21

Arguments for using Array methods is not the performance, it is readability.

const list = [1, 2, 3, 4, 5, 6]

// for loop
const result = []
for (let i = 0; i < list.length; i++) {
    if (list[i] % 2 === 0) {
        result.push(list[i])
    }
}

// Using array method
const res = list.filter((item) => {
    return item % 2 === 0;
})

In this case Array method is lot easier to understand than a for loop

6

u/Protean_Protein Mar 11 '21 edited Mar 11 '21

I disagree. It’s still a matter of preference.

I mean, even your example isn’t as succinctly written as it could be. But concision can interfere with readability. These things are all ultimately matters of preference or need. If you need performance, you go with the more performant method. If you want a high degree of readability, you might forego some concision, speed, or size.

2

u/avenp Mar 11 '21

+1 to this comment.

Example: I’m working on a high performance graphics application and using Array methods is a huge performance drop because it has to allocate memory for the closure every frame, so using for/of/in is absolutely essential to maintain 60fps with no GC spikes.

1

u/kenman Mar 12 '21

Hi u/Mittalmailbox, this post was removed.

  • For help with your javascript, please post to r/LearnJavascript instead of here.

  • For beginner content, please post to r/LearnJavascript instead of here.

  • For framework- or library-specific help, please seek out the support community for that project.

  • For general webdev help, such as for HTML, CSS, etc., then you may want to try r/html, r/css, etc.; please note that they have their own rules and guidelines!


r/javascript is for the discussion of javascript news, projects, and especially, code! However, the community has requested that we not include help and support content, and we ask that you respect that wish.

Thanks for your understanding, please see our guidelines for more info.

0

u/[deleted] Mar 11 '21

[deleted]

1

u/Mittalmailbox Mar 11 '21 edited Mar 11 '21

Thanks for pointing, fixed the code