JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – Find sum of object and variable value

JavaScript Quiz – Find sum of object and variable value

Question –

In this js quiz, you need to find sum of object and variable value. But there is also one thing in it. We are also incrementing a value.

So, First we are declaring a num variable using let keyword and assign number value 5 to it.

In the next step, we create an obj variable using let and assign javascript object to it. And object which we assign to obj contain 2 key value pairs.

First one is just a num key in obj with value 10.

And second one, value key with increment operator num

At, last we are adding three values. Which are…

obj.value + obj.num + num

And assign it to result variable, created using let keyword. So, what will be the result value. Let’s find out.

Answer – 22

Click To Run code

Explanation

Now, you are thinking why the answer is 22.

Let me explain you step by step. So, you can understand the reason behind it.

First, we are calling obj.value for adding and it’s value is 6 because we have used an increment operator at prefix. So it increment the value before use.

Note : num in value key is a num variable which is declared at top.

If you want to know more about JavaScript Increment Operator

In second step, we are adding a obj.num whose value is 10 and do not confuse with num variable. Both are different, one is a key in object and another one is a variable.

Now at last we are using num variable and it’s value is 5 but as we are already running a increment operator on it using obj.value. It’s value become 6.

So, it’s something look like it.

let result = 6 + 10 + 6

Finally, it gives us the result of 22.

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 – What really the array values method return
  2. JavaScript Quiz – Does nested array reference removed by spread syntax
  3. JavaScipt Quiz – What is the length of String Split Method
  4. JavaScript Quiz – Is the calling function inside function are equal

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