DEV Community

Cover image for I thought I knew JavaScript.
Amir
Amir

Posted on

I thought I knew JavaScript.

I had studied:

  • arrays and objects
  • loops and conditionals
  • destructuring
  • string methods
  • even map, filter, reduce

But every time I opened a “simple” problem, my brain froze.

Not because of syntax.

Because I didn’t know how to think.

The mistake I kept making

I was asking:

“What code should I write?”

Instead of:

  • What is the input?
  • What shape should the output have?
  • Am I counting, grouping, or transforming data?
  • Should the result be {} or []?

That single question — {} or [] — blocked me more times than syntax errors ever did.

The moment things clicked

I stopped rushing to write code.

Before touching the keyboard, I started writing this in plain English:

  • “I need to group items by X”
  • “I need to count how many times Y appears”
  • “I need one value per key”

Only then did loops, accumulators, and dynamic keys make sense.
_What was the moment JavaScript finally “clicked” for you?
_


js
const result = {};

for (const item of data) {
  const key = item.type;
  result[key] = (result[key] ?? 0) + 1;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)