Converting switch to when in javascript

#1

I have written whole Android project on Kotlin . In kotlin i have converted many long switch statement to when statement which is short concise and readable , but now in my javascript project i want to do the same thing that is use the when instead of switch .

Please tell anyone how this is possible as i have to convert many lines of code in switch.

#2

As far as I know, JavaScript switch statements have a pretty similar syntax to those in Java, so I don’t really see what is the problem.
The only problem I could think of is that there is no Js -> Kt converter, so you’ll have to re-implement them manually.
However, due to the fact that the syntax is pretty similar it is possible that if you paste a JavaScript switch statement in IntelliJ it will think that it is a piece of Java code and converts it to Kotlin.

#3

Thanks but I want write kotlin when statements in javascript project.

#4

I wouldn’t be surprised if you could trick the Java->Kotlin converter into thinking that your JS is Java.

If that doesn’t work, you could try converting it using regular expressions. For example, here’s a simple one I threw together that’ll replace case statements with Kotlin’s syntax: