Category:JavaScript Variables

From Q
Jump to navigation Jump to search
Related Online Training modules
Simple JavaScript Formulas
JavaScript If Statements
Generally it is best to access online training from within Q by selecting Help : Online Training

Related Videos
Part 5 of Creating and Modifying Variables (Video)

JavaScript Variables are variables created in Q using the JavaScript programming language. They are the most flexible and powerful type of variable that can be created in Q.

Refer to:

Creating a JavaScript variable

JavaScript variables are created by:

Numeric if creating a Numeric Variable.
Text if creating a Text Variable.
  • Filling in the fields:
Name A short description of the variable containing no spaces or special characters.
Label The Label used in reporting.
Expression A JavaScript formula. This formula is applied to each observation (i.e., each row in the Data tab. Any variable in the project can be used to construct an expression by selecting the variable from the Variables list and pressing DownSelect.png, which will cause the variable’s Name to appear in the Expression box wherever the cursor is located. All standard mathematical operations can be used (e.g., +, /, *, brackets, etc). Examples of formulas include:
if (age < 39) 1; else 2
(brandA + brandB + brandC) / daysInYear
See JavaScript Reference for more information about how to write JavaScript and JavaScript Variables Examples Library for examples relating to the creation of variables.
  • Pressing OK.

They are also created automatically when using various other functions in Q (e.g., if creating a Banner question).

Functions available in JavaScript variables

All normal JavaScript functions are available (e.g. isNaN()). See Q's JavaScript Library for others added by Q.

Accessing variables as arrays

By default, all JavaScript functions work for each respondents separately. For example, the JavaScript expression q3 + q4 creates a new variable which contains the sum of the two input variables for each observation. A more powerful (but more complex) way of writing JavaScript treats each of the variables as an array (i.e., a vector). To access this more powerful method, create a new JavaScript variable and check Access all data rows (advanced). See Access All Data Rows for more information.

Editing a JavaScript variable

Right-click on the variable in the Variables and Questions tab and select Edit Variable.

Updating a JavaScript variable

JavaScript variables automatically update whenever the data is changed (e.g., when variables used in the formula are recoded or when new data is imported).

JavaScript Numeric Variables and missing values

If you have missing values for variables used to in the Expression you do not explicitly address this using the isNaN function or if statements, observations in the constructed variable will have missing values for any of the observations for which there are missing values on any of the variables used in their construction.


JavaScript Variables Examples Library contains examples of the use of JavaScript for solving various problems. In particular, the following examples have relatively detailed explanations and are useful to read as a way of understanding how to create JavaScript Variables:

Automatically creating JavaScript variables

Variables can be automatically created by:

Buttons, options and fields

Name The name of the variable to be created.

Label The label of the variable to be created.

Expression Box in which the formula is entered for constructing a variable. All standard mathematical operations can be used (e.g., +, /, *, brackets, etc). Examples of formula include: 1, age / 2 and (brandA + brandB + brandC)/daysInYear. Formulas are written in JavaScript. See JavaScript Reference and JavaScript Variables Examples Library.

Variables List of variables that can be used in the Expression box. Selected by pressing the down arrow. Right-clicking on a variable opens the Value Attributes dialog box for the variable.

Use variables from all data files When your project has multiple data files, you may check this box to use variables from the other data files in the project. This will include the variables in the list below, and allow you to include them within your JavaScript calculations.

Common Methods Double-click a method to insert it into your formula. Click the Help button to see explanations and examples of each method. If you have arrived here from Q, please see JavaScript Reference.

Also see How to Create a Date Variable.

Show Source Value (ignores recoding in Value Attributes) Q's default is to show the Source Values in the Result column of the preview, which are taken directly from your JavaScript expression. This ignores the Current Values and Recode Rules set in the Value Attributes dialog for this variable. Un-check the checkbox to see the recoded output. This checkbox only appears if there are recoded values.

Accelerate (skip logic checks) If ticked then JavaScript variables will run faster, particularly when your code is simple. However local variables set when running one case will remain set for subsequent cases, which can lead to subtle bugs. If you tick this then ensure you initialize local variables before use. For example, write var sum = 0; instead of var sum;.

Access all data rows Treats the variables as arrays; requires that the Expression returns an array. See Access All Data Rows.

Check Code Checks that the formula in the Expression box contains no errors.

Buttons shown when creating variables from a Banner's context menu

The following buttons are shown if you right-click on a Banner table and select BANNER > Add Category - Logic... or BANNER > Add Variable > JavaScript formula....

BtnPreviousVar.png Closes the current variable and opens the previous variable in the banner. If you have unsaved changes, you will be asked to save before continuing. (Shortcut: CTRL+Up)

BtnNextVar.png Closes the current variable and opens the next variable in the banner. If you have unsaved changes, you will be asked to save before continuing. (Shortcut: CTRL+Down)

Save Saves the edited variable. Use this when you want to save the current expression, but want to keep this window open and continue making changes. (Shortcut: CTRL+S)

New Saves the current variable and then starts editing a new blank variable (afterwards, you can click the left arrow button to come back to the current variable). (Shortcut: CTRL+N)