JS.Evaluate
-----------

Evaluates a JavaScript expression.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [JavaScript](component_JavaScript.md) | [10.0](newinversion100.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "JS.Evaluate"; JSRef; JavaScript ) 

**MBS**( **"JS.Evaluate";** /\* Evaluates a JavaScript expression. \*/  
**$JSRef**; /\* The JavaScript reference number.e.g. $js \*/   
**$JavaScript**) /\* A JavaScript expression.e.g. "1+2" \*/ 

### Parameters

| Parameter | Description | Example |
|---|---|---|
| JSRef | The JavaScript reference number. | $js |
| JavaScript | A JavaScript expression. | "1+2" |

### Result

Returns JSON or error.

### Description

Evaluates a JavaScript expression.  
This may include definition of functions and objects and using them.  
Result is converted to JSON and returned.  
  
The evaluate runs within the FileMaker process. If you cause an endless loop, FileMaker freezes and needs to be force quit, so please always include an exit condition for your loops. Also if you get JavaScript to crash, FileMaker will crash with it.   
### Examples

Evaluate some expressions:

 Set Variable \[ $js ; Value: MBS ( "[JS.New](JSNew.md)" ) \]   
Set Variable \[ $r ; Value: MBS ( "JS.Evaluate"; $js ; "4+5") \]   
Show Custom Dialog \[ "Result" ; $r \]   
Set Variable \[ $r ; Value: MBS ( "[JS.SetGlobalProperty](JSSetGlobalProperty.md)"; $JS ; "test"; "\\"Hello World\\"" ) \]   
Set Variable \[ $r ; Value: MBS ( "JS.Evaluate"; $js ; "test + test;") \]   
Show Custom Dialog \[ "Result" ; $r \]   
Set Variable \[ $r ; Value: MBS ( "[JS.Release](JSRelease.md)"; $JS ) \]  
Defines and runs a function in JavaScript:

 Let(\[  
f = "  
function makeCRCTable(){  
 var c;  
 var crcTable = \[\];  
 for(var n =0; n &lt; 256; n++){  
 c = n;  
 for(var k =0; k &lt; 8; k++){  
 c = ((c&amp;1) ? (0xEDB88320 ^ (c &gt;&gt;&gt; 1)) : (c &gt;&gt;&gt; 1));  
 }  
 crcTable\[n\] = c;  
 }  
 return crcTable;  
}  
  
function crc32(str) {  
 var crcTable = crcTable || (crcTable = makeCRCTable());  
 var crc = 0 ^ (-1);  
  
 for (var i = 0; i &lt; str.length; i++ ) {  
 crc = (crc &gt;&gt;&gt; 8) ^ crcTable\[(crc ^ str.charCodeAt(i)) &amp; 0xFF\];  
 }  
  
 return (crc ^ (-1)) &gt;&gt;&gt; 0;  
};  
  
crc32(InputValue);";  
j = MBS ( "[JS.New](JSNew.md)" );  
r = MBS ( "[JS.SetGlobalPropertyValue](JSSetGlobalPropertyValue.md)"; j; "InputValue"; "Hello World" );  
r = MBS ( "JS.Evaluate"; j; f );  
x = MBS ( "[JS.Release](JSRelease.md)"; j )  
\];r)  
Initialize JavaScript environment with two functions:

 If \[ Length($$js ) = 0 \]   
 Set Variable \[ $$js ; Value: MBS ( "[JS.New](JSNew.md)" ) \]   
 Set Variable \[ $r ; Value: MBS ( "JS.Evaluate"; $$js ;   
"function makeCRCTable(){  
 var c;  
 var crcTable = \[\];  
 for(var n =0; n &lt; 256; n++){  
 c = n;  
 for(var k =0; k &lt; 8; k++){  
 c = ((c&amp;1) ? (0xEDB88320 ^ (c &gt;&gt;&gt; 1)) : (c &gt;&gt;&gt; 1));  
 }  
 crcTable\[n\] = c;  
 }  
 return crcTable;  
}  
  
function crc32(str) {  
 var crcTable = crcTable || (crcTable = makeCRCTable());  
 var crc = 0 ^ (-1);  
  
 for (var i = 0; i &lt; str.length; i++ ) {  
 crc = (crc &gt;&gt;&gt; 8) ^ crcTable\[(crc ^ str.charCodeAt(i)) &amp; 0xFF\];  
 }  
  
 return (crc ^ (-1)) &gt;&gt;&gt; 0;  
};") \]   
End If  
List global properties:

 MBS ( "JS.Evaluate"; $$JS ; "Object.keys(globalThis);" )  
Query now in JavaScript:

 MBS ( "JS.Evaluate"; $$JS ; "performance.now();" )  
Measure tikem for a test function:

 MBS ("JS.Evaluate"; $js ; "function testFunction() {  
 for (var i = 0; i &lt; 1e6; i++) {}  
}  
  
var t1 = performance.now();  
testFunction();  
var t2 = performance.now();  
  
('test took: ' + (t2 - t1) + ' milliseconds');")  
### See also

- [FM.Evaluate](FMEvaluate.md)
- [JavaScript.EvaluateScript](JavaScriptEvaluateScript.md)
- [JS.AddFileMakerEvaluateFunction](JSAddFileMakerEvaluateFunction.md)
- [JS.AddFileMakerRunScriptFunction](JSAddFileMakerRunScriptFunction.md)
- [JS.AddFileMakerSQLFunction](JSAddFileMakerSQLFunction.md)
- [JS.EvaluateToString](JSEvaluateToString.md)
- [JS.New](JSNew.md)
- [JS.Release](JSRelease.md)
- [JS.SetGlobalProperty](JSSetGlobalProperty.md)
- [JS.SetGlobalPropertyValue](JSSetGlobalPropertyValue.md)

### Example Databases

- [JavaScript/JavaScript libPhoneNumber](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/JavaScript/JavaScript%20libPhoneNumber.shtml#10ScriptAnchor_)
- [JavaScript/JavaScript SunCalc](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/JavaScript/JavaScript%20SunCalc.shtml#10ScriptAnchor_)
- [JavaScript/JavaScript](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/JavaScript/JavaScript.shtml#2ScriptAnchor_)
- [List/Sort Benchmark](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/List/Sort%20Benchmark.shtml#5ScriptAnchor_)

### Blog Entries

- [FileMaker and JavaScript - the perfect combination](https://www.mbsplugins.de/archive/2020-02-03/FileMaker_and_JavaScript_-_the/monkeybreadsoftware_blog_filemaker)
- [CRC function in FileMaker using JavaScript](https://www.mbsplugins.de/archive/2020-01-15/CRC_function_in_FileMaker_usin/monkeybreadsoftware_blog_filemaker)
- [Our JavaScript engine for FileMaker](https://www.mbsplugins.de/archive/2019-12-09/Our_JavaScript_engine_for_File/monkeybreadsoftware_blog_filemaker)

### FileMaker Magazin

- [Ausgabe 4/2021, Seite 20](https://filemaker-magazin.de/neuigkeit/4152-Appetithappen-FMM_202104)

This function checks for a license.

Created 7th December 2019 , last changed 8th July 2024

  
[JS.CallFunctionValues](JSCallFunctionValues.md) - [JS.EvaluateToString](JSEvaluateToString.md)

[HTML Version](JSEvaluate.shtml)