You can set up scripting expressions that involve values of different types without the compiler raising an exception. Instead, one of the types will automatically be changed (coerced) to that of the other before performing the operation. The compiler will allow all coercions unless it can prove that the coercion will always fail. Any coercion that may fail generates a warning at compile time, and many produce a runtime error if the coercion fails.
For example, if you add a string “Forsta“ and the number 8.5, the number will be converted to a string so that the expression
Forsta8.5will give the string Confirmit8.5 the concatenation of the strings Forsta and 8.5.
In JScript .NET you can also force what type a value should be coerced to by using the target type name (go to About Data Types for more information). JavaScript, however, does not have the idea of explicit data types.
The table below shows what the result will be when using the operator + between values of different types. It illustrates what the order of precedence within the types. If a string is involved, the other types are converted to string. For numbers it will give the number as a string, the Boolean value false will give the string false and true the string true. null will be converted to the string null. An int is converted to a float in an expression with a float value. Boolean true will be converted 1 and false to 0 when used in an expression with a numeric value. null will be converted to 0 when used in an expression with a numeric value.
| row + column | string "12.34" | int 123 | float .123 | logical true | logical false | null |
| string "test" | test12.34 | test123 | test0.123 | testtrue | testfalse | testnull |
| int 123 | 12312.34 | 246 | 123.123 | 124 | 123 | 123 |
| float .123 | 0.12312.34 | 123.123 | 0.246 | 1.123 | 0.123 | 0.123 |
| logical true | true12.34 | 124 | 1.123 | 2 | 1 | 1 |
| logical false | false12.34 | 123 | 0.123 | 1 | 0 | 0 |
| null | null12.34 | 123 | 0.123 | 1 | 0 | 0 |
As all values returned from questions in Forsta Plus are strings, and strings have precedence over all other types, you may have to use some sort of conversion function to change the type before using the value from a question in your scripts.
Find the result of this expression.
(As will be shown in the next part of the documentation, expressions within brackets will be calculated separately before the resultant value is concatenated with the other strings, so e.g. 192+15=207 is the resultant value from the first part that is brought into the string.)
(192+15)+" is not the same as "+(true+206)+", but this isn't really "+true
See the answer in APPENDIX A Answers to Exercises.