# ToScript

Creates a JavaScript expression that assigns the input BoxLang value to a JavaScript variable.

This is useful for converting BoxLang values to JavaScript variables in a script. It can convert:

* Booleans
* DateTimes
* Numbers
* Arrays
* Queries
* Strings
* Structures

## Method Signature

```
ToScript(cfvar=[any], javascriptvar=[string])
```

### Arguments

| Argument        | Type     | Required | Description                                                                                 | Default |
| --------------- | -------- | -------- | ------------------------------------------------------------------------------------------- | ------- |
| `cfvar`         | `any`    | `true`   | The value to convert to a script line; String, DateTimes, Number, Array, Structure or Query |         |
| `javascriptvar` | `string` | `true`   | The name of the JavaScript variable to assign the result to.                                |         |

## Examples

### Multiple to Script Examples

Converts boxlang objects to JavaScript syntax for use

[Run Example](https://try.boxlang.io/?code=eJy1Vm1vGjkQ%2Fr6%2FYmqkFnSkKG0%2FEUBKIZFaNa0SUHunKhJedgCny5ra3gA63X%2Fv%2BGXZJUty7ekaJWHtmXk8M36eYafTadRbvhpM5HimxNr0OrSIaOv1YCize1RGZAvgoI2yD%2FdcCR6nSG6vB1Ev3nY1GjBLocfeoQ9sQiug3yKIgfeUuVnnxj4PJkusBhWoIDJ4K7cpzxa9TjzoxWoQNUq%2Fhtvw%2FwhaZgsH5HFBzqEoolnGtIHd6c9csVavE2I87N4XjjhD68hZ7zIq6T2%2F5z4QtPtow8z3qVrQ56IgIysREc%2BSIltDiSvUeWqqDegeJtnzR4DZrbHPDG5N547Q%2FC4D26ecL8hUnsEGEYHBv5Z3FiVylq8wMy83ShhsepO9gUqJQneBwR%2FgbK2ziNJzBkqtU7nRGl2oU0rxXWDJs5OTExgq5AbBtmAt13nqFiAzPEkEpaGFzHjqw4D8S3Ktdudusw%2Fu8yNumnAKrUCqVMo1JZ3gts8Eg7mSqz47ZdT4PnvDaihfQcDtAUkdNqZoW8HgOdlDcRbZllZnbqDoPrNzX2sgrC%2F3i22qu2WihyFsbQlq10XUEdKXhT9VVsPl%2FAGz5h6r1aBS3WL3tSEat11oVAsOZC6r%2BhkNBQBHGvdUk5DZc6zu%2B5iCSnKVwimqILEcCGzfIUsax1IQ5oXed%2FQ3iOVoIWfRXCpoCntTYbsV%2FV0TUNFxKxhBf%2By2EI%2FfF7d20%2BbKSEr%2FVMXk8n84cr%2FnqIKEHFs1zgyppGibdmyayzSVGxvg3EHGd%2BRmWxlLs4Qvo9Gf5KNW3E%2BfcwcxLjtV7beP1S9dOoHH1w6VQ8INj7nGQ21muNGkJne0EyYTSdsIkyKjFgq6pgWqNvV4tnRjB8pQF3OeJDeSwhxOzTpGM8Q09WaLl1jU0yM4Dz33KYxwgztIcE7DR8NE5Sue%2FU95vPqVPK4wgw1Pv9HAgyspH%2BTgCDTKV%2BtKBqH%2FKs%2FgWs6voRnud%2B5PapUX4Q0ZXxG3tVQGE7oKzSCJvRScnagO9DO%2B%2BHAxnADdErjc4PLm05U71Jk%2F3YwubuDtX8E4uhgP%2FdhwGIGodl54WpSTyyWSUAVWtAdp2EqCTCtUW2CGir4CEoh35cgpw6hllrruGNbqPjp4Homwoq2Pn187vyoVj9qOLnmq8WfTOQIAcwtwOBv9t2PxDlHXLdR0G%2FY9Af7T1KtMvCc6%2BKjbU5UdvCS42i5peMYK74WhuWrfRgC3fLUmfiUSNWTSQK4PBpFndDH%2Bdai0%2BsYxnU5%2FAF6Aku0%3D)

```html
<h2>ToScript</h2>

<h3>Converting a string variable</h3>
<bx:set thisString = "This is a string" >
<bx:output>
<b>The thisString variable in Boxlang</b><br>
#thisString#<br>
<br>
<strong>The output of ToScript(thisString, "jsVar")</strong><br>
#ToScript( thisString, "jsVar" )#<br>
<br>
<strong>In a JavaScript script, convert thisString Variable to JavaScript
and output the resulting variable:</strong><br>
<script type="text/javascript" language="JavaScript">
var #ToScript( thisString, "jsVar" )#;
document.write("jsVar in JavaScript is: " + jsVar);
</script>
</bx:output>

<h3>Converting an array</h3>
<!--- Create and populate a one-dimensional array --->
<bx:set myArray = ArrayNew( 1 ) >
<bx:loop index="i" from="1" to="4">
<bx:set myArray[ i ] = "This is array element" & i >
</bx:loop>

<bx:output>
<b>The Boxlang myArray Array</b><br>
<!--- Write the contents of the myArray variable in Boxlang --->
<bx:loop index="i" from="1" to="#arrayLen( myArray )#">
myArry[#i#]: #myArray[ i ]#<br>
</bx:loop>
<br>
<strong>The output of ToScript(myArray, "jsArray")</strong><br>
#toScript( myArray, "jsArray" )#<br>
<br>
<strong>In JavaScript, convert myArray to a JavaScript variable and write it's contents</strong><br>
<script type="text/javascript" language="JavaScript">
var #ToScript( myArray, "jsArray" )#;
for (i in jsArray)
{
document.write("myArray[" + i + "]: " + jsArray[i] + "<br>");
}
</script>
<br>
<h3>Converting a query</h3>
This section converts the following query object to both WDDX format
and ActionScript type JavaScript objects.<br>

<!--- Query a database --->
<bx:set news = queryNew( "id,title", "integer,varchar" ) >
<bx:set queryAddRow( news ) >
<bx:set querySetCell( news, "id", "1" ) >
<bx:set querySetCell( news, "title", "Dewey defeats Truman" ) >
<bx:set queryAddRow( news ) >
<bx:set querySetCell( news, "id", "2" ) >
<bx:set querySetCell( news, "title", "Men walk on Moon" ) >
<bx:set writeDump( news ) >

<!--- run QofQ (query of query) --->
<bx:query name="sortedNews" dbtype="query">
    SELECT id, title FROM news
    ORDER BY title DESC
</bx:query>
<br>
The Query in Boxlang
<bx:dump var="sortedNews">

<strong>JavaScript generated by ToScript(sortedNews, "WDDXQuery"):</strong><br>
#toScript( sortedNews, "WDDXQuery" )#;<br>
<br>
<strong>JavaScript generated by ToScript(sortedNews, "ActionScriptQuery",
False):</strong><br>
#toScript( sortedNews, "ActionScriptQuery", false )#<br>
<br>
<!--- Convert to both WDDX format and ActionScript format --->
<script type="text/javascript" language="JavaScript">
#ToScript( sortedNews, "WDDXQuery" )#;
#ToScript( sortedNews, "ActionScriptQuery", false )#;
</script>
<!--- For brevity, this example does not use JavaScript query variables --->
</bx:output>
```

### Additional Examples

[Run Example](https://try.boxlang.io/?code=eJyNkcFqwzAMhs%2F1UwifUgik3XFhh8BGobCM4rFL6cENJvNo3E6213Wl7z7ZzlYCYewiWb%2Blzz9yUYBw6BvHUoI7OF%2FKvljDHDak8O3%2BcydNy0v2Zl8kkrQUT7VQqOVOf6msZ8C0ZKjsL%2Bl5LxrUB5eBjVIOPM7z0HhE7dS97w4ZXGdILwqoEOWJxUiU9aZM5%2F%2FbSaPJDZ0HVuLdH05Cf7Kx8gpPLEYivIdcq2MG3MhO5bJVnDAfEptXibnxHb3fkHRmk7p6fIBb4MJbzXM2qRahvJmxS29qFZFXU%2FERosU8Yir0J1O0KG3a8EFhE05Zx3%2BWPiBSTTyK48vuabXvtgqZiYnm57MII3kAS%2FfES4cRZJiYlt%2FuR7jz)

```java
// Struct
Struct = {};
Struct[ 1 ] = "boxlang";
jsVar = JSONSerialize( Struct );
resStruct = ToScript( struct, "jsVar" );
writeDump( resStruct );
// Array
Array = [];
Array[ 1 ] = "boxlang";
jsVar = JSONSerialize( Array );
resArr = ToScript( Array, "jsVar" );
writeDump( resArr );
// Query
Query = queryNew( "name,age", "varchar,numeric", {
	NAME : "Susi",
	AGE : 20
} );
resQry = ToScript( Query, "Query" );
writeDump( resQry );
// String
Str = "test";
resStr = ToScript( Str, "Str" );
writeDump( resStr );
// Number
number = 10;
resNum = ToScript( number, "number" );
writeDump( resNum );

```

RESULT: jsVar = {"1":"boxlang"}; jsVar = \["boxlang"]; Query = {"columns":\["name","age"],"data":\[\["Susi",20]]}; Str = "test"; number = 10;

## Related

* [DataNavigate](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/datanavigate)
* [JSONDeserialize](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/jsondeserialize)
* [JSONPrettify](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/jsonprettify)
* [JSONSerialize](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/jsonserialize)
* [LSParseNumber](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/lsparsenumber)
* [ParseNumber](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/parsenumber)
* [ToBase64](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/tobase64)
* [ToBinary](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/tobinary)
* [ToModifiable](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/tomodifiable)
* [ToNumeric](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/tonumeric)
* [ToString](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/tostring)
* [ToUnmodifiable](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/tounmodifiable)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/conversion/toscript.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
