Last active
July 5, 2019 20:52
-
-
Save ssaadh/98b435af03c011b9f7fc7964fc7ed10e to your computer and use it in GitHub Desktop.
Javascript nested ternary vs switch case. Which is better?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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