JavaScript Tips

From Q
Jump to: navigation, search

This page illustrates some of the more basic ways that JavaScript works. Refer to JavaScript for a general overview of JavaScript in Q, and to JavaScript Reference for links on how to write JavaScript in Q.

On this page, v1, v2 and v3 are names of variables in Q.

JavaScript is case sensitive

Q’s scripting language, JavaScript, is case-sensitive. For example, dog does not equal Dog.

Comments

// Comments are ignored by JavaScript, but are a great help to you.

Write lots of comments to document what your JavaScript is meant to do. A comment starts with //. This is a great help if you are trying to figure out your script months later, or if someone else is reading your script.

Logical operators

Operator Description
&& and
|| or
! not

Relational operators

Operator Description
== equals. For example, v1 == v2 will return a true if v1 and v2 are the same. Use only for strings and numbers; it will not work when comparing arrays or objects. Use equals() to compare objects.
!= not equals. Use only for strings and numbers; it will not work when comparing arrays or objects. Use equals() to compare objects.
< less than
<= less than or equal to
> greater than
>= greater than or equal to

Boolean expressions

A Boolean expression is an expression in the Expression which evaluates as a true or false. Results that are true are returned as values of 1 and false as 0 (i.e., this is a way to construct a binary variable).

For example:

  • v1 != v2 returns a 1 for observations where v1 and v2 differ, since v1 != v2 is true, and a value of 0 otherwise, since v1 != v2 is false.
  • isNaN(v1) returns a 1 for observations for v1 that have the value of NaN and a value of 0 otherwise.

if statements

As discussed above, by default Boolean expressions return values of 1 and 0. This can be overridden by using an if statement. For example: if (age < 35) 1; else 0 . Or: if (isNaN(v1)) 99; else 26.

See If and else if Statements for more information.

Special things only available in Q

isNaN Returns a 0 if the variable is not missing and a 1 if the variable is missing (e.g., isNaN(v2)).
Q.AsNumeric Converts a Text Variable containing numbers into a Numeric Variable. If you change Variable Type from Text to Numeric, Q automatically constructs a new numeric variable using JavaScript. For example, if the original variable was called v1, the new variable would have Q.AsNumeric(v1) as its Expression.
Q.Label Converts a variable's value into its corresponding label. e.g. 2 might become "Female".

Mathematical functions

All the standard mathematical functions are available in JavaScript (e.g., Math.floor(a) and Math.max(a)). See JavaScript Mathematical Functions for more information.

Alerts

if (result > 100)
    alert('My algorithm returned a result greater than 100, but it is supposed to be a percentage! The result was: ' + result);

If a result does not look like what you expect, it is helpful to understand what your JavaScript is doing. Use the alert function to show a message box at any time, and include any interesting values in the message. This only works with Table JavaScript and Plot JavaScript and QScript (i.e., not when creating variables). If wanting to achieve the same outcome in JavaScript Variables just return what you wish to view in the last line of your expression.

Errors and Stack Traces

When an error occurs, Q will tell you which line of your program the error occurred on. If the error occurred inside a function you have defined (or imported using includeFile() or includeWeb()) then Q (version 4.7+) will display a stack trace. A stack trace shows you which line your program was on when it called the function, and which line in the function (or functions) it was on. The first line shows where the error occurred. The second line shows the function that called that code, and so on.

Error: There are no data files in the project.  Use project.addDataFile()
    at firstVariable (C:\Users\bob\Desktop\Fiddling.QScript:5:17)
    at findVariables (C:\Users\bob\Desktop\Fiddling.QScript:64:5)
    at C:\Users\bob\Desktop\Fiddling.QScript:117:1

In this example the problem occurred in firstVariable function (line 5, column 17), which was called by the findVariable function (line 64, column 5), which was called from line 117 in the script.

TAB and formatting

Bad:

if (result < 10) {
if (result < 5)
result = 5;
else
result = 10;
}

Good:

if (result < 10) {
    if (result < 5)
        result = 5;
    else
        result = 10;
}

Use the Tab key on your keyboard to make your code more readable. Whenever you write an if/else/for line, use Tab on the next line.