DEV Community

loading...

Discussion on: Week 7 of JS Bootcamp!

Collapse
functional_js profile image
Functional Javascript • Edited

Hey Melissa,
Way to keep rewiring those neurons! :)

It'll all sink in over time.
There's an old express... "Nothing is so full of victory as patience."

As for the boolean type (a type that can only represent one of two values (mutually exclusive); true or false)


I never use "true" or "false" explicitly in JS (javascript)

//not good
if (x === true) {
// do something
}


I also never do this, but you will see this idiom commonly used by many...

//not good
if (x) {
// do something
}

//not good
if (!x) {
// do something
}


To handle truthy and falsy values, I use predicate functions (functions whose return type is a boolean)
There's a naming conventions for these type of funcs, which is to prefix them with "is" and sometimes "has")

//good
if (isArray(arr1)) {
// do something
}


A more complex boolean set (using either && and ||) should be evaluated before the "if header" to be more readable.
(here is a somewhat contrived example for demo purposes)...

//good

const isValidData = isArray(arr1) && isNotEmptyArr(arr1) && isArrOfNumbers(arr1);

if (isValidData) {
// do something
}
Collapse
codetivated profile image
Melissa Heying Author

Thanks so much for your time in typing all that out! We’re starting to use booleans like your last example!

Forem Open with the Forem app