JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – variable can be key and value in object

JavaScript Quiz – variable can be key and value in object

JavaScript Object

JavaScript object is a collection of key-value pairs where any kind of data type value can be store. An object is like an array but the array has only a number key as an index where objects have named keys and methods also..

Mostly everything in javascript is an object like dates, math, arrays, function, etc.

There are two ways you can declare an object, using a new keyword or object literal.

It’s syntax looks like –

  1. new Object()
  2. {}

Want to know more about javascript objects click here.

JavaScript Question

In this javascript quiz code, find out what happen when we just place a variable in javascript object literal as object need key value pair.

First we declare a variable name using let keyword and assign a value “JS Startup”.

let name = “JS Startup”;

In the second step, we created an another variable object using let and assign an object literal to it. And also passed name variable as key and value.

let object = { name : name };

Variable As Key In Object –

Now, created another variable newObj using let and assign an object with only the name variable inside object literal.

let newObj = { name };

Finally, we are checking the value of the object variable for the name key and we get the output “JS Startup”. As we know that happens because we assign key values.

console.log(object[‘name’]); // output = “JS Startup”

But what happen with last console log does it print the same output or something else.


Answer – “JS Startup

Click To Run Code


Now, you know the answer is “JS Startup“. But how it is possible, as object need key value pair. Let me explain.

So it’s right object need a key value pair. As we have assign in object variable name as key and value also which is name variable.

But there is an important feature of object that when the key is same as variable name then you can only pass variable.

That time, your variable name becomes key and its value becomes key value. But in this process, we can not change the key name to do this you have to use the previous statements like in the object variable.

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

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. JS Quiz – copy object with JSON stringify and parse method
  2. JavaScript Quiz – can isFrozen method really check freeze object
  3. JS Quiz – new set object can return the same size
  4. JavaScript Quiz – can break statement work on array forEach method


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
Share via
Copy link