MZ How do I check if skill user is a certain enemy in the database?

As mentioned in the title, I try to check if a certain enemy is using a skill through JS. Something like this but for enemies.
if (user == $gameActors.actor(x))
A couple of ideas:
  1. Check their position in their unit (0 = leader), e.g.
    JavaScript:
    if (user.isEnemy() && user.index() === 0) alert('enemy leader used a skill!');

  2. Check their database ID, e.g.
    JavaScript:
    if (user.enemyId?.() === 5) alert('enemy ID 5 used a skill!');
A couple of ideas:
  1. Check their position in their unit (0 = leader), e.g.
    JavaScript:
    if (user.isEnemy() && user.index() === 0) alert('enemy leader used a skill!');

  2. Check their database ID, e.g.
    JavaScript:
    if (user.enemyId?.() === 5) alert('enemy ID 5 used a skill!');
Maybe I'm wrong too. But these examples are for a skill formula, aren't they? I actually want to check in a notetag who is currently using this skill. This skill can be used by actors and enemies and there is an actor who is also represented as an enemy and if he uses this skill I want to change the cost.

JavaScript:
<JS TP Cost>
if (user == $gameActors.actor(12)) {
    cost = 0;
} else {
    cost = $gameVariables.value(297) * 1;
}
</JS TP Cost>

That is what I am currently using. However, this only takes into account the actor and not the enemy, who should also not have any costs for the skill.
But these examples are for a skill formula, aren't they? I actually want to check in a notetag who is currently using this skill.
The conditions I suggested should work in any context where user refers to an instance of Game_Battler, i.e. an actor or an enemy. This does not, by default, include item/skill formulae. Variables available in scripted notetags will vary depending on how the corresponding plugin is coded.

This skill can be used by actors and enemies and there is an actor who is also represented as an enemy and if he uses this skill I want to change the cost.
If you want to check for a specific enemy ID (case 2), I think something like this should work:
JavaScript:
if (user === $gameActors.actor(12)) {
    cost = 0;   // actor ID 12
} else if (user.enemyId?.() === 34) {
    cost = 0;   // enemy ID 34
} else {
    cost = $gameVariables.value(297) * 1;
}
In case it's confusing you: ?. is the optional chaining operator. It's a shorthand to check if user.enemyId exists before trying to evaluate it. Alternatively you could write something like this:
JavaScript:
if (user.isEnemy() && user.enemyId() === 34) {
Works perfectly fine. Thanks for the good and detailed explanation. I think I understand the topic a lot better now. But since I have multiple enemies I was going with this.

JavaScript:
<JS TP Cost>
if (user === $gameActors.actor(12) || user.enemyId?.() === 25 || user.enemyId?.() === 26 || user.enemyId?.() === 27 || user.enemyId?.() === 28 || user.enemyId?.() === 29 || user.enemyId?.() === 30 || user.enemyId?.() === 31) {
cost = 0;
} else {
cost = $gameVariables.value(297) * 1;
}
</JS TP Cost>

Original thread: https://forums.rpgmakerweb.com/threads/how-do-i-check-if-skill-user-is-a-certain-enemy-in-the-database.138648/