Https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/pjs-javascript-capstone/projects/lodash

I am a touch confused about something and i am struggling to pin it down in any documentation.

i am working on the lodash recreation project at Lodash project.

I am working on the has() method

The docs suggest doing something like this

const hasValue = object[key] !== undefined;
    return hasValue;

which i simplified to

return object[key] ? true : false;

Happy as tests pass with this.

My editor threw me a curveball then and suggested this was unneccesary and that it could be done like this.

return !!object[key]; 

test pass with this too. Could someone explain to me how this works though? I am not overly sure what is going on here. Cannot seem to pin down what !! does

Thanks in advance.

If object[key] evaluates to a boolean when placed in the context of a ternary operator or conditional (as it seems to in your initial simplified version), then it should also do so when being affected by conditional operators (in this case, a series of two ! [not] operators).

So this feels like a weird “hacky” way to force object[key] into a boolean return by converting it into its opposite (either existing or not existing) and then back into what it was originally, but in a boolean state.


After a bit more research, I found some additional information on StackOverflow that seems to support my original “hypothesis”. But an interesting case to be sure.

1 Like

Thankyou, i understand now what is going on. I mean it really doesn’t read well and it also doesn’t make much sense outside of trying to be clever, as it doesn’t cover something then being null or 0.

for the sake of the task, the supplied conditional would be the only one that would cover the case correctly?

my slightly shorter version could also throw an error if provided with null or 0 if i am not being dense.

“Truthy” vs. “Falsy” values are applicable here, if I understand your question correctly:

Falsy values are defined as false, 0, "", null, undefined, NaN. So the way the double bang operator would affect anything with those values would be different.

Edward Plato did a good writeup of the JS Double Bang that I think will clear things up further.

Yeah i think i got it from the start of your reply tbh but i will definitely check those articles out.

I am glad i figured this out, because even though i get tests green, it didn’t feel like it was a good way.

I am also glad that it was legitimately as complicated as i thought :smiley:

1 Like