Why Does Kotlin/JS Return Different Results for === Than Does Kotlin/JVM?
This is because of how the runtime handles it.
On the JVM, ==
maps to equals
, and ===
maps to ==
(identity checking), as outlined here. Meanwhile, JavaScript's equals operators are weirder. If you decompile your code, you get this with JS:
kotlin.kotlin.io.output.flush();
if (typeof kotlin === 'undefined') {
throw new Error("Error loading module 'moduleId'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'moduleId'.");
}
var moduleId = function (_, Kotlin) {
'use strict';
var equals = Kotlin.equals;
var println = Kotlin.kotlin.io.println_s8jyv4$;
function main(args) {
var value = 'something';
println(equals(value.toUpperCase().toLowerCase(), value)); // NOTE: equals
println(value.toUpperCase().toLowerCase() === value); // NOTE: ===
}
_.main_kand9s$ = main;
main([]);
Kotlin.defineModule('moduleId', _);
return _;
}(typeof moduleId === 'undefined' ? {} : moduleId, kotlin);
kotlin.kotlin.io.output.buffer;
Now, if you consider the equivalent Java code (slightly shortened and without Kotlin):
public static void main(String[] args){
String value = "something";
System.out.println(value.toUpperCase().toLowerCase().equals(value));
System.out.println(value.toUpperCase().toLowerCase() == value);
}
toUpperCase().toLowerCase()
creates a new object, which breaks the ==
comparison, which is identity checking.
While ===
is outlined as identity checking as well, a === b
is true if a and b are strings that contain the same characters. As you can tell from the decompiled Kotlin code, Kotlin.JS compiles to primitive Strings, not String objects. Because of that, the ===
in JS will return true when you're dealing with primitive strings.