Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Use of 'const' in ExtendScript

Explorer ,
Jun 19, 2013 Jun 19, 2013

Let's start with an example.

const a = 1;

a = 2;

$.writeln(a); // Prints 1.

Here we can see that 'const' does indeed work. If you want to run this more than once in the ExtendScript Toolkit, be sure to select Debug > Reset from the menu.

Another example:

$.strict = true;

const a = 1;

a = 2; // ReferenceError: a is read only

If strict mode is disabled, reassigning a constant value fails silently. If strict mode is enabled, reassigning a constant value throws an error.

Here's where it starts to get weird:

(function() {

    const a = 1;

    a = 2;

    $.writeln(a); // Prints 2.

})();

Huh? Suddenly 'const' has no effect! Even strict mode doesn't cause the reassignment to fail.

In the earlier examples, "a" is actually a member of $.global. So I got this bright idea:

var object = {};

object.a = null;

with (object) {

    const a = 1;

}

object.a = 2;

$.writeln(object.a); // Prints 1.

"a" is constant again! The "with" block locks the value of "a" so it can't be reassigned.

So here's the general rule: Only object properties can be made constant. Local variables cannot be made constant. This means that closure variables cannot be made constant.

To double-check this, I put the previous code inside a function:

(function() {

    var object = {};

    object.a = null;

    with (object) {

        const a = 1;

    }

    object.a = 2;

    $.writeln(object.a); // Prints 1.

})();

No problem!

Note that this behavior is not at all like JavaScript. In fact, it's the exact opposite of JavaScript. But still, it's very useful!

Since "with" is considered harmful, it would be nice if we could create constants without using "with". However, due to the syntax requirements of "const", I don't think there's any getting around it.

TOPICS
Scripting
3.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 19, 2013 Jun 19, 2013

hello!

a quick couple of questions:

In what have you tested this?

As far as i know, in Indesign's implementation of ExtendScript (CS5 at least), strict mode is ignored.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 20, 2013 Jun 20, 2013

Adobe ExtendScript Toolkit CS6

$.version: 4.2.12

$.build: 66.489054

$.buildDate: Mon Dec 05 2011 15:11:07 GMT-0500

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 26, 2013 Jun 26, 2013

I discovered an unexpected and undesired side-effect of using constants in ExtendScript:

var object = {};

object.a = null;

with (object) {

    const a = 1;

}

object.a = 2; // Throws "ReferenceError: a is read only" if strict mode is enabled.

$.writeln(object.a); // Prints 1.

// As a side effect, the local variable 'a' now exists and holds the value 'undefined'.

Even though the 'const' keyword was being applied to 'object.a', as a side effect the local variable 'a' was declared. To my knowledge it's impossible to delete local variables, so there's no getting around it. This means that making a global variable constant is effectively useless (though that seems like it would be a bad idea anyway).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 20, 2025 Oct 20, 2025

I'm new to scripting and i've been testing my hand in phtoshop and i've noticed some things. Using var in older versions of photoshop works perfectly, but using it in newer versions seems to throw some errors sometimes, its not consistent. So maybe i was wondering if its because im using a cracked version of photoshop or not. I'd really appreciate your feedback

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 20, 2025 Oct 20, 2025
LATEST

Hi @midnite_ops , This discussion is from 2013, so you might want to start a new thread either here (InDesign) or in the Photoshop forum. If you are wondering about the original question, constant is read only, while var is read/write. So:

 

const MAGIC_NUMBER = 1;

//throws an error, MAGIC_NUMBER is read only
MAGIC_NUMBER = 2;

var mn = 1
mn = 2

// returns 2, var is read/write and can be changed anywhere in the code
$.writeln(mn)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines