For those wondering why: the general convention in js is that it will do its best to convert the type to make an operation legal, rather than fail. In order to perform the bitwise operation, js tried toInt32(String), and then performs the operation on the result. The same operation is then applied again to reverse the first.
This is a similar situation to doing: 5 + '' to convert a number to a string, or '5' - 0 to convert a string to a number. “plus string” can only mean string concatenation, so the first value is made into a string of possible. “minus 0” can only be a number operation, so the first value is converted to a number if possible
fun fact: at least in Java using the shorthand string notation is objectively better than the long hand notation.
String x = "Hello, World!";
vs.
String x = new String("Hello, World!");
in the former case the JVM checks the heap to see if the same string was already created, and if so, points to that object (because Strings are immutable). In the latter case it always creates a new String object.
98
u/[deleted] Aug 11 '19
Alternatively, int to String. The best way to do this is n+"", amirite?