Re-creating the Lodash Library - Implement _.has() - Need help

Many thanks for your answer mtf. It did help :slight_smile:

On .has() I’d have a question too. I have the following code:

has(object, key) {
return object.key != undefined ? true : false

and it does not pas the test although it’s not dissimilar to your code further above. The terminal says that ‘has was not proper defined’. Is this because I did not include any keys in the object for the method to run on?

Thanks a lot

Don’t think so… It may have to do with the dot notation. Did you try using a subscript?


yeah, I did. same result. I don’t think the code is correct though, I’m firing it at a console with a test object to see what it does and keep getting stupid error messages of unexpected tokens and the rest of it. frustrating.

Errors can appear elsewhere that trip things up. Please post your entire _ object so we can inspect it.

Something else to consider.

return object[key] != undefined ? true : false

This is not a case where a ternary is of any importance. The expression is already boolean.

return object[key] !== undefined;

will return true or false. Note the operator is strict. Avoid using loose comparisons, == or != as this can lead to potential problems down the road.

Hi mtf,

follow-up on this. This code worked:

has(object, key) {
return object[key] !== undefined

but this one didn’t:

has(object, key) {
return object.key !== undefined

I will steer clear of dot notation from now on when it comes to accessing keys in an object.

Many thanks…


Hi all,
I have this kind of situation could someone figure out what happens here? And why I have this error?

The last line in the editor window seems awry. Should it be something like this,

module.exports = _;


Hi, I was also stuck on question 21 :

My code worked when I used instead of dot notation as you showed. But I was reading through the replies here and I’ve never come across:

What does the - ? and - true : false syntax mean? I can’t find explanations of it on google.
My answer:

    var hasValue = true;
    if (object[key] !== undefined){
     return hasValue = true;
    } else {
      return hasValue = false;
    return hasValue;

Even though its passed on the console, I didn’t feel I’d gained enough understanding on this one. I was also stuck with the interaction between variables and conditional statements/booleans. Can you not directly assign a variable to an if statement? Or in other words what are the rules for assigning variables to conditional statements?
I was getting errors for:

var hasValue = if (object[key] !== undefined){
return true;
} else {
return false;
return hasValue;
Any help would be great

Look up ternary expression statement, which is similar to if..else and can even be expanded to emulate, if..else if..else though it can get messy and convoluted.

ternary means three.

condition ? the_if : the_else;

? is referred to as the ternary operator, and the colon is a required separator token between the two case actions.

We know that only a truthy evaluation makes it past if, and defaults to else for all other cases.

No, since if has no return value, only one of a single or more actions that occur within its blocks.

Ah ok the ternary operator makes sense, thanks again.

I sort of understand what you mean by the lack of return value in an if statement. But why does a function allow variables to be assigned to it directly if it also comprises of actions?
They both use the keyword return?

Functions are objects, as in they are values. IF is a control flow construct and not an object as such. We can return a function, but not an if statement.

Regardless what goes on inside a function, the caller sees only the return value.

const ab = (a, b) => a * b;

We see ab(6, 7) but the caller sees 42.

Now ternary expressions on the other hand are values, so we can assign them.

a = 6; b = 7;
console.log(ab(a, b) === 42 ? "One factor is divisible by 3" : "Not sure if any factors are divisible by 3");
// One factor is divisible by 3

That last line is what the console sees, and logs it.

Because a ternary is a value, we can return it, unlike an if statement.

1 Like

Ok, so I think I understand: an empty function is in a sense still an object - acting as a placeholder for results/values anyway. But an empty if statement on its own is unrecognizable to a variable?

Precisely. A function, being an object, can be referenced. Try this…

const print = console.log

print("Do you think this will log to the console?")

We just assigned a reference to method, and now invoke it using that reference. This is not something we can do with IF (or WHILE, or SWITCH) as they are control flow devices, not objects.

There is no way to write,

a = if(b) {


Not just an empty function, any function. They are objects, and as such, values.

1 Like

line 4 should be
let clampedValue = Math.min(Math.max(number, lower), upper);
return clampedValue;

1 Like

hiya! reading your responses throughout this project has helped me a ton! So, thank you! i seem to be stuck at this point. I’m not sure how to word my code in part 21.3. ( 1. Within the method, create a variable called hasValue and set its value equal to the result of checking to see if the current value of object at key does not equal undefined .) I tried const hasValue = (object.valueOf(key) === !undefined);
** return hasValue;** this got me a lot closer but I’m not quite there. I know the answer is in the instruction itself, but I am just not seeing it. any help you can provide would be much appreciated!


Equal to NOT is the same as not equal to, and likely easier logic to sort through.

The code doesn’t work if I use:

has(object, key) {
    let hasValue = object.key;
    if (hasValue === undefined) {
      return false
    } else {
      return true

but does work for:

has(object, key) {
    let hasValue = object[key];
    if (hasValue === undefined) {
      return false
    } else {
      return true
  • the difference being that I originally tried to access the object property with dot, rather than bracket, notation.

Can anyone help out with why I need to use the bracket notation to access the object property?


In the above code, key is a variable. The only way we can use dot notation is if the property name ‘key’ actually exists.


must be true. We can assume it will be false so fall back to subscript notation. The same applies when we iterate over an object…

for (key in object) {
    console.log(key, ':', object[key])

'node test/lodash.js’ is exactly what the video instructor for this course recommends doing. Is she wrong?

I would have to watch the video to be able to comment. My memory of the module is that we write each method, in turn, then test that method…

node test/dropwhile.js

and so on. The test shown in your post would be centered on testing the entire object. Check the test directory and see if there is such a test in list.