# JavaScript

Even though you can put more Javascript code in the main.js file and do a lot of things there, sometimes we want to do something in some part of our game. This is why you can also put some Javascript inside your game's script and you have a simple and pro way to do it.

The simple one, is using the $ command:

The syntax is pretty easy: "$ \[Javascript Code]". Here are some examples:

```javascript
// All variables must be declared as a window property. var x = 0; will not work as expected.
"$ var window.x = 0",
"$ x+=1",
"$ alert(x)"
```

The "pro" way is with functions. Yes, you can put a function inside and it will run it!

To control the flow of your game, you can make the function return either true (Will immediately execute the next statement.) or false (Will wait until the user clicks again.)

Example:

```javascript
"Hi there!",
function(){
    alert("This is pretty useful!");
    return true; // Will make the engine execute the next statement when the function finishes.
},
"Isn't it?"
```

You can as well use variables in your script.

```javascript
"The result of 2+2 is: " + (2+2),
"Isn't that right?"
```


---

# 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://developers.monogatari.io/documentation/1.3.0/script/javascript.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.
