Here is a more realistic example of the switch statement; it converts a value to a string in a way that depends on the type of the value: function convert(x) { switch(typeof x) { case "number": // Convert the number to a hexadecimal integer return x.toString(16); case "string": // Return the string enclosed in quotes return '"' + x + '"'; default: // Convert any other type in the usual way return String(x); } }
Example