We compared and contrasted the use of variables between JavaScript and FileMaker. Now, we want to talk about functions and how to use JavaScript in FileMaker.
Claris's FileMaker platform and most spreadsheets have these useful features called functions. These functions do simple calculation jobs for us. For example, the Trim() function removes white space from the end of a text block, and the Round () function rounds a number to the nearest specified digit. Their uses are practically limitless. More likely than not, we developers rely on functions like these to get our jobs done daily.
FileMaker
Trim ( "This is a test " ) = This is a test
Floor ( 99.999) = 99
or
Set Variable ( $amt ; 99.999)
Floor ( $amt ) = 99
Well, JavaScript also offers very similar functions, but they're called methods. Just like FileMaker, we can use methods to modify, change or analyze some piece of data. Another similarity is that we can feed a method either a variable or the data directly. As we discussed in the previous post, be sure to use single quotes instead of double quotes with variables in JavaScript.
JavaScript
'this is a test '.trim(); = This is a test
var $answer = Math.floor(99.999); = 99
or
var $amt = 99.999;
var $answer = Math.floor($amt); = 99
Just as FileMaker has its field types, JavaScript has a collection of different global objects that we can apply to methods:
Now here's the confusing part, JavaScript also has something called functions. But they're more akin to complex calculations or FileMaker's custom functions, even more so since Claris introduced the While function in FileMaker 18. These JavaScript functions often call methods and sometimes other functions, much like FileMaker custom functions call other custom functions and standard functions. Remember, custom functions are available in FileMaker once we check the "Use advanced tools" option in the Preferences section. In addition, both JavaScript functions and FileMaker custom functions can accept parameters, and they operate similarly.
FileMaker
Set Variable ( $amt ; 99.999 )
Set Variable ( $plus ; 5 )
Set Variable ( $answer ; FloorPlus ( $amt ; $plus) = 104
FloorPlus Custom Function:
Name: FloorPlus
Parameters: FloorAmt ; PlusAmt
Calculation: Floor( FloorAmt ) + PlusAmt
JavaScript
var $amt = 99.999 ;
var $plus = 5 ;
function FloorPlus ( amount , plus ) {return Math.floor(amount) + plus; }
var $answer = FloorPlus($amt, $plus)
As we can see, FileMaker and JavaScript aren't that different, as long as we understand them. The release of FileMaker's While function helped close the gap between the two even more. We can use FileMaker's While function to create a loop that calls itself while the calculation condition remains true. This function eliminates a lot of the recursive reasons we might have used a custom function in the past.
It's easy to assume that there would be more correlations between JavaScript and FileMaker scripting exclusively. But, that's not necessarily the case. There are indeed plenty of parallels that JavaScript shares with FileMaker scripting. For example, both tools, working with their respective user interface (UI) counterparts – CSS /HTML for JavaScript and Layouts for FileMaker – can create complex workflows that include branching logic, user input, and feedback. But a lot of JavaScript's power comes from its ability to change UI elements using functions and methods, just like the FileMaker tool itself can hide and conditionally format layout objects using its functions.
JavaScript has a lot of the same stuff we're already familiar with using in FileMaker. Hopefully, this short series inspires us to explore how to use JavaScript in FileMaker to extend its capabilities.
Keep up with the latest FileMaker features and application development trends by joining our mailing list. And, if you need any help working with JavaScript in your custom applications, feel free to explore our FileMaker mentoring and consulting services.
This article is also published on FileMakerProGurus.com.