Skip to content

Instantly share code, notes, and snippets.

@thejsj
Created November 21, 2016 03:06
Show Gist options
  • Save thejsj/b3e0e8d032374a707a418b3d9a7b86c5 to your computer and use it in GitHub Desktop.
Save thejsj/b3e0e8d032374a707a418b3d9a7b86c5 to your computer and use it in GitHub Desktop.
Return vs Else Example
'use strict'
// All these functions behave the same way
const getHello = function (a) {
if (a === true) {
return 'hello'
}
else {
return 'wow'
}
}
const getHello = function (a) {
if (a === true) {
return 'hello'
}
return 'wow'
}
const getHello = (a) => { // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
if (a === true) return 'hello'
return 'wow'
}
const getHello = (a) => {
if (a) return 'hello' // Checks for truthy values instead of true
return 'wow'
}
const getHello = (a) => a ? 'hello' : 'wow' // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment