Introduction
This article describes useful methods for troubleshooting custom JavaScript code.
Requirements
A JavaScript variable, Rule or QScript.
Tips
You can improve readability by formatting your code as follows:
-
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.// Comments are ignored by JavaScript, but are a great help to you.
- 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.if (result < 10) {
if (result < 5)
result = 5;
else
result = 10;
}
When writing JavaScript code, you need to be exact and pay attention to:
- The presence or absence of semi-colons: they signal that you’re ready for the next bit of the expression.
- The capitalization of letters: case-sensitivity means that dog does not equal Dog or DOG.
- The right number of brackets (and where they open/close).
- Whether brackets are (round) or {curly} or [square].
- When symbols are doubled: double ampersands (
&&
) for “AND”, double-equal signs (==
) for equality, and double pipes (||
) for "OR". - Index values: JavaScript is 0-based so the first iteration in a loop or array is 0, not 1.
- Missing values with your input variables: they can lead to missing results if not handled correctly. For example, if you are summing a set of variables (e.g.
q1 + q2 + q3
) and one of them returns a NaN (Not a number) for a particular record, the result for that record will also be NaN unless you explicitly address this using theisNaN
function or if statements.
See Also
How to Use JavaScript in Displayr
Comments
0 comments
Article is closed for comments.