How to Fix JavaScript Else If Operator Syntax in PDF Form Calculations
Question details
The user needs to fix invalid JavaScript syntax in a PDF form to calculate values dynamically based on numerical conditions.

- Product
- Device & OS
- not provided
- Scenario
- Creating dynamic PDF forms that calculate a Terms of Sales value based on the number of ordered items.
- Observed behavior
- The conditional JavaScript code fails to evaluate ranges correctly and defaults to incorrect outputs due to invalid operator syntax.
Ensure you have access to your PDF form's editing tools and the JavaScript properties window to test your script modifications.
Correct JavaScript Comparison Operators in the PDF Form
Replace assignment operators with comparison operators and fix syntax errors to ensure accurate conditional logic.
The issue usually stems from using a single equals sign (=) instead of a double equals sign (==) for comparison, or using incorrect greater-than-or-equal-to (>=) syntax in the else if conditions. In JavaScript, a single equals sign assigns a value rather than checking if a condition is true.
Open your PDF form in edit mode, right-click the target field where the calculation happens, and select 'Properties'. Navigate to the 'Calculate' tab and edit the custom calculation script.
Find the if/else if block in your JavaScript code that is failing to evaluate the ranges properly.
Change any variable assignment operators (=) inside the condition parentheses to comparison operators (==). For example, change 'if(v = 0)' to 'if(v == 0)'.
Ensure that the greater-than-or-equal-to operator is written exactly as '>=', without spaces or flipped characters.
Update your script to reflect the correct logic. Example: var v = Number(this.getField('PlatesOrdered').valueAsString); if(v == 0) event.value = 'At Time Of Order'; else if(v > 0 && v < 20) event.value = 'At Time Of Order'; else if(v > 19 && v < 100) event.value = 'Net 15'; else if(v >= 100) event.value = 'Net 30';

Edit and Manage PDF Forms with WPS Office
Easily view, edit, and fill interactive PDF forms using WPS Office. Its comprehensive PDF tools allow you to manage form data without complex setups, offering a lightweight environment for all your document needs.
- 1. Install WPS Office: Download and install the free WPS Office suite on your device.
- 2. Open your PDF document: Launch WPS Office and open the PDF form you need to manage.
- 3. Interact with form fields: Navigate to the 'Form' tab or simply click on the interactive fields to input your data or verify calculations.
- 4. Save your changes: Click 'Save' to securely preserve the filled data directly within your PDF.

Frequently Asked Questions
Why does my PDF form calculation always return the last else statement's value?
This typically happens when the conditional statements contain assignment operators (=) instead of comparison operators (==). The condition inadvertently assigns the value and evaluates as true, bypassing the intended logical check.
How do I retrieve a field value as a number in a PDF JavaScript calculation?
You can retrieve a field's value using 'this.getField("FieldName").valueAsString'. To ensure it is treated strictly as a number for mathematical comparisons, wrap the entire statement in the Number() function, like 'Number(this.getField("FieldName").valueAsString)'.
Can I use logical operators like AND in PDF JavaScript?
Yes, you can use the logical AND operator (&&) to check multiple conditions within an 'else if' statement. This is especially useful for determining if a field's value falls within a specific numerical range.




