How to Declare an Array's length?

Hello!
I recently started an experiment with Script Calls to try and create a "chess-like" Strategy RPG (not a serious project, just curious to see how much i was capable to realize with Scripts).

The idea was that the battles happen map, and not on the standard battle screen. To do that, i created a Script that saved in Variables the actor's parameters, like this:

Spoiler: Script
◆Variable:#0035 IDActor = 0
◆Variable:#0036 Index = 0
◆Variable:#0033 nCharacters = 4
◆Variable:#0034 Looping = 1
◆Loop
◆Script:var member = $gameVariables.value(36);
: :$gameVariables.setValue(35, $gameParty.members()[member]._actorId);
◆Variable:#0037 Paramater = 0
◆Loop
◆Script:var loop = $gameVariables.value(34);
: :var id_actor = $gameVariables.value(35);
: :var stat = $gameVariables.value(37);
: :$gameVariables.setValue(loop, $gameActors.actor(id_actor).param(stat));
◆Variable:#0034 Looping += 1
◆Variable:#0037 Parameter += 1
◆If:Parameter > 7
◆Interrupt Loop
◆
:End
◆
: Repeat
◆Variable:#0033 nCharacters -= 1
◆Variable:#0036 Index += 1
◆If:nCharacters = 0
◆Interrupt Loop
◆
:End
◆
:Repeat

Where $gameVariables.setValue(loop, $gameActors.actor(id_actor).param(stat)) is there to scale to the variables that will contain each of the 4 party members' parameters, and this one works!

But then I realized that, if i wanted to do this for the Enemies, i would need to create arrays, since i don't want the enemies to appear in the same number every battle.

So my plan was that, during battle, all the enemies appearing would share ONE Variable Array for each parameter, and that Variable would alter its length according to how many enemies are there.

So the question is this: is it possible to script something like "var enemy_stat[] = new var [n_enemies];"?
To create an empty array with a specific length you can use:

var myArray= new Array(length)

Then you can assign values to it using:

myArray[índex]=value
You can create a variable that is an array with a length based on the current number of enemies. You can do that using this:
Code:
var myAry = new Array(length);
Of course, after creating it, you have to set each element of the array somehow before using it. Otherwise you are going to use undefined elements.
If you want to learn more about Arrays in JavaScript I recommend taking a look at the documentation.

EDIT: Ninjed
I wasn't so far off then.
Thank you very much peq42_, i'll try it immediately!
And thank you too Heirukichi, i'll check that out too!

EDIT (Sorry for the Doubleposting): If anyone is interested, here's the complete process i used to store enemies' stats:

Spoiler: Script
◆Variable:#0036 Index = 39
◆Variable:#0037 Stat = 0
◆Loop
◆If:n_Bandits > 0
◆Script:var n_enemies1 = $gameVariables.value(49);
: :var enemystats1 = new Array(n_enemies1); var stat = $gameVariables.value(37);
: :var i = 0;
: :do
: :{
: : enemystats1 = $gameVariables.value(47)[stat];
: : i += 1;
: :}
: :while(i < n_enemies1);
: :$gameVariables.setValue(52, enemystats1);
◆
:End
◆If:n_Archers > 0
◆Script:var n_enemies1 = $gameVariables.value(50);
: :var enemystats1 = new Array(n_enemies1); var stat = $gameVariables.value(37);
: :var i = 0;
: :do
: :{
: : enemystats1 = $gameVariables.value(48)[stat];
: : i += 1;
: :}
: :while(i < n_enemies1);
: :$gameVariables.setValue(53, enemystats1);
◆
:End
◆Script:var n_enemies = $gameVariables.value(38);
var n_enemies1 = $gameVariables.value(49);
var n_enemies2 = $gameVariables.value(50);
: :var enemystats = new Array(n_enemies);
: :var i = 0; var n = 0; var index = $gameVariables.value(36);
: :do
{
: : enemystats[n+i] = $gameVariables.value(52); i += 1;
: :}
while(i < n_enemies1);
: :n = i; i = 0;
: :do
{
enemystats[n+i] = $gameVariables.value(53); i += 1;
: }
while(i < n_enemies2);
: :$gameVariables.setValue(index, enemystats);

◆Variable:#0036 Index += 1
◆Variable:#0037 Stat += 1
◆If:Index ≤ 46
◆
:Else
◆Interrupt Loop
◆
:End
◆
:Repeat

Where "n_Bandits"(Variable 049) and "n_Archers"(Variable 050) are altered at every battle to count how many bandits and/or archers there are.
Variables "047" and "048", instead, are a sort of library, containing the stats of a specific archetype of enemy:

Spoiler: Script
◆Script:var banditstats = [5, 1, 4, 3, 2, 3, 3, 4];
:Script:$gameVariables.setValue(47, enemystats);
◆Script:var archerstats = [4, 3, 3, 3, 2, 2, 5, 5];
:Script:$gameVariables.setValue(48, enemystats);
Double-post notice: Randor01
I have in this instance merged your two posts together as it makes more sense, given the content. If you want to add something, just use the Edit button on your earlier post and then include the new info/comment.
Arrays in javascript are highly flexible things. You don't really need to set or change an array's length (although you can) in order to add or remove enemies from it:

var enemies = []; // creates an empty array, ready for enemies
enemies.push(new Enemy()); // The Array.push method lets you add an object to your array.
enemies.splice(n, 1); // The Array.splice method lets you remove an enemy object from your array.

So in practice:

var enemies = []; // new array with length 0
enemies.push(troll); // array is now length 1
enemies.push(goblin); // array is now length 2
enemies.push(slime); // array is now length 3
enemies.push(ooze); // array is now length 4
enemies.splice(2, 1); // removes "slime" (index 2), array is now length 3 again

Edit: If for whatever reason you want to change an array's length, you can do this:

enemies.length = 5; // sets the array's length to 5

It's generally not necessary to do this, though. Increasing the size adds undefined elements. decreasing the size lops off elements that would extend past the new length.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-declare-an-arrays-length.111548/