Basic Array Not Working

Hello,
I am new to Javascript. However, I am not totally new to coding in general and I am stumped on something that should be extremely basic. This:

var cars = ["ford", "volvo", "saab"];
var first_car = cars[0];

...does not work. It says the variable "cars" is not defined. I don't understand. I read through tutorials and basically copy and pasted this, yet it doesn't work. Are arrays handled differently in RPG Maker MV than in basic Javascript? Sorry if this seems insanely rudimentary, but I'm at a road block--arrays are essential to my game's progress.

Thank you.
You would have to show us the code @jglag as it stands; I can't say for certain if the variable cars is not defined.
I can assure you that arrays are no different between regular JS and MV.
You would have to show us the code @jglag as it stands; I can't say for certain if the variable cars is not defined.
I can assure you that arrays are no different between regular JS and MV.
Hi Kino,
The code is exactly what I posted above. I wanted to make sure that part worked before I did anything else, and even that crashes the game.

When I talk to an event, that code is supposed to run, and instead it says that "cars" is undefined.
I just did what you did here: http://prntscr.com/fiy9py

This is the exact same thing. Are you using a script command to call your code?
I just did what you did here: http://prntscr.com/fiy9py

This is the exact same thing. Are you using a script command to call your code?
Mine is identical to yours except that I didn't put in console.log. Is that necessary?
Yes, using script command, like yours.
Nope; the console.log is for me to check if everything is working and to show it in the developer console.
If you want to bring up the developer console on play test, press F8.

It'd be easier to make sure with a screenshot though; little things can cause errors.
https://prnt.sc/fiyccy
Yea, the issue is you have two different script commands. Mine is only one.
Script commands can span multiple lines. If you declared a variable called cars in one script command, by the second one it's already gone.

You could think of each script command you make as a JavaScript function, meaning once you make a new one all the variables from the old one can't be accessed.
Yea, the issue is you have two different script commands. Mine is only one.
Script commands can span multiple lines. If you declared a variable called cars in one script command, by the second one it's already gone.

You could think of each script command you make as a JavaScript function, meaning once you make a new one all the variables from the old one can't be accessed.
Thank you! This is something very different from GML, which I have extensive knowledge in.

Just so I know, is there a way in RPG Maker to make a global or persistent array variable that lives beyond that script, or even that event? I know I can set an in-game variable, but I'm not sure if they can be arrays...
If you wanted to make it persistent within that event; there are plugins for that, but doing it manually with JavaScript would definitely be more involved.

For adding something globally to MV? you can access at any point. You can write
Code:
window.myarray = ['volvo', 'honda'];
//This is just an example, but make sure you do window.<yourvariablename> = <value>
// Like the above example.

If you were making a plugin, instead of doing it in a script call, it would be:
Code:
var myarray = [];
//In this case, the plugin you created for RMMV is added to the global/window context.
//This means you can access it from anywhere in the game, while the game is running only.
Lovely. Thanks for all the help!
You're welcome; good luck with your project!
@jglag
the problem you faced had nothing to do with differences between GML and javascript - it has to do with the event command "script".

For security as well as performance reasons, the script command in events is sandboxed - deleting all references to it when it ends.
A plugin is nothing but a javascript file that gets added to the general code, and that is NOT sandboxed - which means that your ideas will work there, but you can damage the engine if you aren't carefull with your plugin code (something that is not possible by event script command).

Original thread: https://forums.rpgmakerweb.com/threads/basic-array-not-working.79867/