JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – can array really be access with an array

JavaScript Quiz – can array really be access with an array

JavaScript Array Access

In javascript, array is a non-primitive type and can store any type of data type element and then in order to access those element an index is used.

JavaScript Array Syntax –

new Array()
or
[]

Example of JavaScript Array –

["String", 1, ["Array", "Index"], { name : "object" }, true, false]

For more detail about the javascript array click here.

JavaScript Question

In this javascript quiz, find out can array be access with an array because in order access array value and index is pass to array.

First, we declare a variable numbers using const keyword and assign javascript array to numbers variable.

Javascript array only contain the numbers.

const numbers = [2, 4, 6, 8, 10];

But at same time we are also access the values of array but not passing single index but multiple like array.

const numbers = [2, 4, 6, 8, 10][1, 2];

Now, we are checking the value numbers variable and also checking the value at index 0.

console.log(numbers[0]); // output => ?
console.log(numbers); // output => ?

So, friends any idea what should be the output of above statement.

JavaScript Quiz Test

const numbers = [2, 4, 6, 8, 10][1, 2];

console.log(numbers[0]); // output => ?
console.log(numbers); // output => ?

Answer – undefined, 6

Click To Run Code

Explanation

Now, you know the answer is “undefined, 6” But why this is the answer, and what does [1, 2] is doing after an array. Let me explain

As, it correct that for access javascript array values. We need to pass the index number for particular value.

But only the number value passes to the array which is single. But when multiple values are passed it looks for the last value.

So, Let’s take an example

const numbers = [2, 4, 6, 8, 10][1, 2];

console.log(numbers); // output =>  6

And when it access at 0 index of number it’s undefined because number not a array any more.

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

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 – switch case can change a variable value
  2. JS Quiz – zero date can give get full year method
  3. JS Quiz – does set object really store duplicate object
  4. JavaScript Quiz – what does the async array map function return

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