Skip to content

Instantly share code, notes, and snippets.

@ssaadh
Last active July 5, 2019 20:52
Show Gist options
  • Save ssaadh/98b435af03c011b9f7fc7964fc7ed10e to your computer and use it in GitHub Desktop.
Save ssaadh/98b435af03c011b9f7fc7964fc7ed10e to your computer and use it in GitHub Desktop.
Javascript nested ternary vs switch case. Which is better?
# Easier to copy paste from what I was doing. Working on a React Component.
# Was working with React and d3. So the functions are imported from d3.
import { axisTop, axisRight, axisBottom, axisLeft } from 'd3-axis';
# Ternary example as a small function
ternaryEx = ( bootstrap ) => bootstrap === 'top' ? axisTop :
bootstrap === 'right' ? axisRight :
bootstrap === 'bottom' ? axisBottom :
axisLeft;
# Switch example as a small function
switchEx = ( bootstrap ) => {
switch( bootstrap ) {
case 'top':
return axisTop;
case 'right':
return axisRight;
case 'bottom':
return axisBottom;
case 'left':
return axisLeft;
# This could be left or whatever you want the default return to be
default:
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment