This is a quirk of the SLAX to XSLT syntax. The easiest way to look at it is to say that expr writes text to the result tree, so if you do this:
expr "Hello World!";
Then that text is written to the result tree, but keep in mind that writing to the result tree does not equal writing to the console. Writing to the console is achieved through special functions like jcs:output() or result tree elements like <output>.
The reason why this works is because you are assigning that text to the <output> element, which Junos uses to write text to the result tree:
<output> {
expr "Hello World";
}
This doesn't work because it is invalid SLAX syntax: <output> expr "Hello Word": expr is only valid as the start of a line.
Typically you would use the expr "string" notation when you want to assign a value to a variable conditionally. This works because you are actually redirection the result tree to a variable, which becomes a result tree fragment data type.
So:
var $result = {
if( $A == $B) {
expr "1";
}
else {
expr "2";
}
}
Is actually writing either 1 or 2 to the result tree, but then redirecting this into the variable $result. $result technically has a data type of result tree fragment rather than string, but you can treat it as a string within your script without any problem because SLAX will automatically convert between data types.
The last topic to mention is that of functions. expr is used in front of function calls simply because it is a requirement of the SLAX syntax. This stems from the translation process from SLAX to XSLT. Basically, without the expr statement it would be difficult for the SLAX converter to detect a leading function call. So, the rule is that any standalone function has to be started by expr:
expr jcs:syslog(...)
expr jcs:output(...)
etc...
Note, that if the function returns any value then that value will be output to the result tree by the expr function. The way I like to think about it is that SLAX has to try to do something with the function return value, even if the function will never return any value. So, if you aren't assigning the function value to a variable, using it as a function argument, or within a location path, then you have to try to write it to the result tree through the "expr" statement.