# Converting switch to when in javascript

**URL:** <https://discuss.kotlinlang.org/t/converting-switch-to-when-in-javascript/12029>\
**Category:** JavaScript\
**Created:** [March 19, 2019, 12:02pm UTC](https://discuss.kotlinlang.org/t/converting-switch-to-when-in-javascript/12029 "2019-03-19T12:02:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gautamkumar268249](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@gautamkumar268249](https://discuss.kotlinlang.org/u/gautamkumar268249)\
**Post date:** [March 19, 2019, 12:02pm UTC](https://discuss.kotlinlang.org/t/converting-switch-to-when-in-javascript/12029/1 "2019-03-19T12:02:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [March 19, 2019, 12:20pm UTC](https://discuss.kotlinlang.org/t/converting-switch-to-when-in-javascript/12029/2 "2019-03-19T12:20:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gautamkumar268249](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@gautamkumar268249](https://discuss.kotlinlang.org/u/gautamkumar268249)\
**Post date:** [March 19, 2019, 1:21pm UTC](https://discuss.kotlinlang.org/t/converting-switch-to-when-in-javascript/12029/3 "2019-03-19T13:21:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![benwoodworth](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@benwoodworth](https://discuss.kotlinlang.org/u/benwoodworth)\
**Post date:** [March 19, 2019, 7:55pm UTC](https://discuss.kotlinlang.org/t/converting-switch-to-when-in-javascript/12029/4 "2019-03-19T19:55:41Z")

</div>

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:

> **[regex101: build, test, and debug regex](https://regex101.com/r/LRJSaf/2)**
>
> Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET.
