Use coercion
For example, in the case that +
receives a string as the first and a number as the second arg, it will coerce the second one to a string and concatenate them. However, if you do it vice versa, with a number as the first and a string as the second, it will convert the second to a number and add the two. This means that you can use +0
to convert a string to a number, and +""
to convert a number to a string.
However, sometimes, +0
can be one byte shorter! E
, the evaluate function, will evaluate its argument as Knight code. So given a number-string, it will convert to a number. Often, however, this will cost 1 or 2 bytes due to whitespace being needed.
With *
, if the first argument is a string and the second a number, it will repeat the string n times instead of multiplying. In the case of num+str, it will coerce the string to a number. And with str+str, it will coerce the second to a number and repeat the first string that many times.
However, with -
, /
, ^
, and %
, if the first is a number, the second will be converted to a number. But if the first is a string, it's undefined behavior.
Some other operators like <
and >
will also coerce - see the spec.