DEV Community

MarcelGeo
MarcelGeo

Posted on

Bug of the day with module variable

Let´s see code bellow:

// Item.js
let active = null;

const setActive = item => {
  active = item;
}

export const activateItem = item => {
  active = setActive(item);
}

Where´s the problem? After several hours of debugging of our js code for activating modules. I found out this bug in similar code as above.
Variable active is global for module. If you will call function activateItem from outside of module, the value of active is undefined. Why? Method setAcive returns nothing:


NOTHING = undefined; // :D

Let's go to repair this situation:

// Item.js
let active = null;

const setActive = item => {
  active = item;
}

export const activateItem = item => {
  setActive(item);
}

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay