# Javascript Piping

Using javascript piping its possible to pipe a value that will be generated using any javascript of your choice.

A special NGSJS function can be setup to return an output of your javascript code, the output can then be stored in a Default value to show it and store it within a field

{% hint style="warning" %}
The output will only be evaluate during submit mode, if you survey answers have been submitted and you are in change mode or resume mode the value will be original one that has been saved prior resuming or changing the answers&#x20;
{% endhint %}

## ➕Simple Fields Sum Example

Below are the steps to add a custom JavaScript that sums two fields. You can modify the code to achieve any desired output to store. Note that if you only need to sum you may consider using the existing [Numeric Field](/form-management/form-designer/answers/answer-types/numeric-answer-field.md) type instead\
\
Create a question with 3 fields

<figure><img src="/files/l51N05ukD699Hf9eaMEU" alt=""><figcaption></figcaption></figure>

Once created for each field involved in the calcualtion set in its answer properties its Pipe alias, this will be used later on to look up the current value.\
\
![](/files/yWN5sY2bOmcxWqNclCsD)\
\
![](/files/ImRAE1BEjteNzlI7FjvY)\
\
\
In the Result field, open the answer settings and in the Default text value use the NGSJS function to get the value of the 2 other fields and output the result\ <br>

<figure><img src="/files/o2dDQAmEXuJEyJzWzf4u" alt=""><figcaption></figcaption></figure>

```
NGSJS(return pipeContext.getAnswer('VALUEA') + pipeContext.getAnswer('VALUEB'))
```

## 👨‍💻️ pipeContext

the pipeContext is a built in object that gives you following options :\
\
**Properties :**

* `questions: Question[]`\
  List of survey questions.
* `answers: Answer[]`\
  List of possible answers across all questions.
* `respondent: Respondent`\
  Current respondent information.
* `panelists: Panelist[]`\
  Associated panelists (if applicable).
* `respondentAnswers: RespondentAnswer[]`\
  Answers provided by the respondent.
* `pipeDataAttributesValues: PipeDataAttributeValue[]`\
  Additional pipe data attributes and values.

#### Methods

**`getAnswer(id: string): string`**

Returns the value(s) associated with a given identifier.

* Supports lookup by:
  * Answer ID
  * Question ID
  * Reporting alias
  * Pipe alias
* Behavior:
  * If the identifier matches a **text-based answer**, returns its value.
  * If it matches a **question**, returns selected answers.
  * Multiple values are returned as a comma-separated string.

**`isSelectedAnswer(id: string, value: string): boolean`**

Checks whether a specific answer is selected.

* Parameters:
  * `id`: Question or answer identifier
  * `value`: Answer identifier or value to check
* Behavior:
  * If `id` refers to a question → checks if the specified answer is selected.
  * If `id` refers to an answer → compares its value directly.
* Returns:
  * `true` if the answer is selected or matches
  * `false` otherwise


---

# 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://docs.ngsurvey.com/form-management/form-designer/piping/javascript-piping.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.
