Simple substitute to Switch Case statements

PHOTO EMBED

Tue Mar 16 2021 01:34:46 GMT+0000 (Coordinated Universal Time)

Saved by @avj2352 #javascript

const method1 = () => console.log('Does something');
const method2 = () => console.log('Does another thing');
const method3 = () => console.log('Does entirely other thing');
 
const switchObj = {
  a: method1,
  b: method2,
  c: method3
};
 
const init = (option) => {
	console.log('instead of switch case, use an object');
  	console.log(switchObj[option]());
}
 
init('a');
content_copyCOPY

Useful alternative to very long switch case statements.

https://educativ.io