# JAVASCRIPT_FUNCTION

The JAVASCRIPT_FUNCTION component allows to execute a custom JavaScript function and output its return value.

Examples of common use cases are:

  • Remove the first n characters from the barcode
  • Remove the last n characters from the barcode
  • Replace parts of the barcode
  • Extrapolate the data encoded inside the barcode
  • ... and much more

You can use pretty much any combination of JavaScript methods to manipulate the strings. You can find the full list in the JavaScript String Methods.

# Examples

# Example 1: Remove the first character of a barcode

Initial barcode:  0897654321
Final output:      897654321

Steps:

  1. Add a JAVASCRIPT_FUNCTION component just after the BARCODE component
  2. Add the barcode.substr(1) code to the JAVASCRIPT_FUNCTION component
  3. Enable the Skip output option of the BARCODE component

# Example 2: Extract a URL parameter from a QR Code

QR Code:        https://example.com/?user_id=123&tracking_id=RN015215156&order_id=456
Final output:   RN015215156

To achieve this result you can use the following code:

new URL(barcode).searchParams.get('tracking_id')

# Example 3: Extrapolate parts of the barcode

Initial barcode:  433-0132-22
Final output:         0132

Use the same procedure of Example 1, but use this JavaScript code instead:

barcode.split('-')[1]

# Using ChatGPT

If you're not familiar with JavaScript you can use AI to generate the code for you.

# Advanced

# Multiline functions

Function that spans multiple lines must be written using the following syntax:

(function(barcode) {
    let result = '';
    /* Your code here */
    return result;
})(barcode)

Important notes:

  • Do not use single line comments // inside the function, only multiline comments /* */ are allowed.
  • You can inject variables by passing them as parameters of the function
  • Do not put the semicolon ; at the end of the function if you want to use the output

# Save global variables

You can store temporarary values to the window object, e.g.:

window['myVar'] = barcode.substr(0, 5)

To recall it inside other components, use the following syntax: {{ window['myVar'] }}.