Question

In javaScript (within Photoshop) when giving a hex colour a value it's in quotes eg "FCAA00"

 var hexCol = "FCAA00";
 var fgColor = new SolidColor;
 fgColor.rgb.hexValue = hexCol

but when passing a variable to that value you don't need the quotes. Why is this?

 var hexCol = convertRgbToHex(252, 170, 0)
 hexCol = "\"" + hexCol + "\""; // These quotes are not needed.
 var fgColor = new SolidColor;
 fgColor.rgb.hexValue = hexCol

Is this just a javaScript quirk or am I missing something going on behind the scenes, as it were. Thank you.

Was it helpful?

Solution

The quotation marks are a syntactic construct which denote a string literal. I.e. the parser knows that the characters between the quotations marks form the value of a string. That also means that they are not part of the value itself, they are only relevant for the parser.

Examples:

// string literal with value foo
"foo" 

// the string **value** is assigned to the variable bar, 
// i.e. the variables references a string with value foo
var bar = "foo"; 

// Here we have three strings:
// Two string literals with the value " (a quotation mark)
// One variable with the value foo
// The three string values are concatenated together and result in "foo",
// which is a different value than foo
var baz = "\"" + bar + "\"";

The last case is what you tried. It creates a string which literally contains quotation marks. It's equivalent to writing

"\"foo\""

which is clearly different than "foo".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top