write a switch in swift in javascript

Switch in Swift

main.swift
let value = 42
switch value {
case 0:
    print("Value is 0")
case 42:
    print("Value is 42")
default:
    print("Value is not 0 or 42")
}
141 chars
10 lines

This switch statement sets the value constant to 42, and then switches over it. Since the case matches the value, it prints "Value is 42".

Switch in JavaScript

index.tsx
const value = 42;
switch(value) {
    case 0:
        console.log("Value is 0");
        break;
    case 42:
        console.log("Value is 42");
        break;
    default:
        console.log("Value is not 0 or 42");
        break;
}
235 chars
13 lines

This switch statement sets the value constant to 42, and then switches over it. Since the case matches the value, it logs "Value is 42" to the console. The break statement is important to ensure that no other cases are evaluated once a matching case has been found.

gistlibby LogSnag