Other great resources: Official JS API docs, Scripting Forum
Constants
Overview
This page is in need of technical review – eg. you can't create custom constants inside function scope in current releases.
Constants are static read-only values – they can't be changed at runtime.
The pre-defined constants provide a reliable and consistent method of communicating static values between your script and the JS API.
There are some constants which the JS API doesn't expose, however there are scripts in the Backport project that will add them for you.
You can also define your own custom constants (see bottom of this page for details) that are unique to your script.
Availability
Constants have been added and removed at various stages of Warzone's evolution. For a summary of availability and changes, see: Constants by version.
Pre-defined constants
I've grouped these by the Object properties or Globals they relate to where applicable...
Custom constants
You can create custom constants using the Javascript 'const' statement like so:
// a global constant that you can use anywhere const researchLab = "A0ResearchFacility"; // a constant that's only available in your function function foo() { const bar = "baz"; // this only exists in function foo }
This is a great way to make your code more readable, especially when working with structure and feature IDs or droid component IDs. If you're looking for those IDs, take a look at Useful Links.
It's customary to put constant definitions at the top of your script. Like the 'var' statement, constants will get hoisted to the top of their function (or global scope if not in a function), but your code will be easier to understand and maintain if you always put them up at the top. If you haven't already done so, I highly recommend reading MDN's Scope Cheatsheet - it's one of the best summaries of scopes and hoisting, and things like that, anywhere on the internet.