Published on 8 April 2021
delete
but it mutates the object, so it's no good for our purposes.Map.drop/2
that do this explicitly. In JS, however, we can use the ...
rest operator and object destructuring to get rid of the properties we don't want.const author = { name: 'Anna', surname: 'Komnene', dob: 1083 }
const { dob, ...authorWithoutDob } = author
console.log(authorWithoutDob) // => { name: 'Anna', surname: 'Komnene' }
ignoreRestSiblings
option for no-unused-vars
quite useful.console
object in JS is the most powerful tool in your debugging toolbelt and it's worth knowing how it works, primarily because it effectively eliminates the need for a dedicated debugger. When you're doing more functional programming in JS you will very often use the feature of arrow functions that immediately allows you to return a value:const plusTwo = x => x + 2
console.log
and explicitly return the value. Too many keystrokes, and difficult to undo in Vim!console.log
(and other console functions) always return undefined
, which is a falsey value. We can combine it with the or (||
) operator to inject a console log before the return value.const plusTwo = x => console.log(x) || x + 2
const distanceInKm = 30
const deliveryPrice = distanceInKm > 15 ? 3.99 : 5.99
const distanceInKm = 30
const deliveryPrice = distanceInKm > 15 ? (distanceInKm > 30 ? 8.99 : 5.99) : 3.99
let
:const distanceInKm = 30
let deliveryPrice
if (distanceInKm > 30) deliveryPrice = 8.99
else if (distanceInKm > 15) deliveryPrice = 5.99
else deliveryPrice = 3.99
let
is lava because it opens up our value to be accidentally reassigned later down the line and it will not be caught by the linter or the interpreter. The solution is to refactor our conditional out to a function (and as a bonus we can use something approximating guard clauses by returning from ifs):const getDeliveryPrice = distance => {
if (distance > 30) return 8.99
if (distance > 15) return 5.99
return 3.99
}
const distanceInKm = 30
const deliveryPrice = getDeliveryPrice(distanceInKm)