JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – object keys values method output can be equal

JavaScript Quiz – object keys values method output can be equal

Object Keys Method –

In javascript, object keys method (Object.keys()) is used to return all the object keys in an array. And all the elements in array which are object keys are in string format.

It’s syntax look like –

Object.keys()

Form more detail about javascript object keys method click here.

Object Values Method –

In javascript, the object values method (Object.values()) also used to return all the object key values in an array. And all the elements in array which are object keys are in string format.

It’s syntax look like –

Object.values()

Form more detail about javascript object values method click here.

JavaScript Question

In this javascript quiz, can object keys values method output can be equal to each other if object have same key value as it return array.

First, we declare variable detail using let keyword and assign an javascript object to it. Which contain only one key.

let detail = { name : “name” };

In the second step, create another variable keys and store the result return from Object keys method used with detail.

let keys = Object.keys(detail);

Now, create a variable value using let and store the result return from the object value method used with detail.

let keys = Object.values(detail);

Finally, we are matching the keys and values variable together using === which is typed equal.

console.log(keys === values);

Any idea what should be the output of the above statement ?

Answer – false

Click To Run Code

Explanation

Now, you know the answer is “false“. But why, doesn’t it give the same array. Let me explain.

First, let see the output object keys values method.

Object.keys({ name : “name” }); // [“name”]

Object.values({ name : “name” }); // [“name”]

So, even they are same but still not equal. Because both the array save at different pointer location.

At last, That’s why we get an answer “false“.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – array single value comparison can be possible
  2. JS Quiz – compare the self value with data type
  3. JS Quiz – can NaN be really equal to NaN
  4. JavaScript Quiz – can use the reserved keyword as variable

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link