DEV Community

Cover image for I thought I knew JavaScript.
Amir
Amir

Posted on • Edited 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 (11)

Collapse
 
mmar58 profile image
MMAR58

This is exactly it! The problem isn't JavaScript; it's the mental model.

I always tell people: if you can't explain how to do the task manually with a pen and paper, you aren't ready to write the code yet. Here is the framework I use to bridge that gap:

1. The Manual Walkthrough

Forget the computer for a second. If I gave you a pile of index cards (your data), how would you sort them? You’d probably create piles on your desk. Those "piles" are your mental variables.

  • Step 1: Look at the card.
  • Step 2: Decide which pile it belongs to.
  • Step 3: Update the count or add it to the stack.

2. The "Variable" as Memory

A computer has no "intuition"—it only knows what you tell it to remember. For every piece of information you need to keep track of, you need a variable.

  • Need to know the total? let total = 0;
  • Need to keep track of names? const names = [];

3. Visualizing Iterations (The "Trace" Method)

Don't just write a loop and hope. See the values changing in your mind (or on paper) after every single step:

  • Iteration 1: key is "Apple", result becomes {"Apple": 1}.
  • Iteration 2: key is "Banana", result becomes {"Apple": 1, "Banana": 1}.
  • Iteration 3: key is "Apple" again, result updates to {"Apple": 2, "Banana": 1}.

4. The Single Responsibility Rule

A function should do one thing. If you are trying to fetch data, filter it, format it, and save it all in one block, your brain will freeze because the "Logic Architecture" is too cluttered. Break it down:

  1. One function to Get data.
  2. One function to Transform data.
  3. One function to Display data.

The Code is just the translation. Once you have the manual steps and the memory (variables) planned out, the JavaScript syntax just becomes the "dictionary" you use to tell the computer what you've already solved in your head.

Collapse
 
amirhossein_ln profile image
Amir

This is an excellent breakdown — especially the “manual walkthrough” part.

What really resonates with me is the idea that variables are just explicit memory. A lot of the confusion I had came from trying to “write code” before deciding what the program actually needed to remember.

The trace method you described is also huge. Once you slow down and force yourself to see each iteration as a state transition, loops stop feeling magical and start feeling mechanical — in a good way.

I like how this frames JavaScript as a translation step, not the problem-solving step itself. The problem is already solved on paper; JS just expresses it.

Collapse
 
mmar58 profile image
MMAR58

Yes, later it will help you learn other language or framework easily.
Because logic or problem solving would be same. Just translation would be different.

And features would be different based on the framework.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.