JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – array single value comparison can be possible

JavaScript Quiz – array single value comparison can be possible

JavaScript Array Comparison –

JavaScript array comparison is all about comparing javascript array with another javascript array. Either directly comparing array or comparing their value.

JavaScript array is a collection of different types of values like string, number, boolean, array, object, and more.

There are ways to declare array.

  1. Array Constructor – new Array(arrayLength)
  2. Array Literal – []

For more information about javascript array click here.

JavaScript Question

In this javascript quiz, find can array single value comparison can be possible and also with number value because the array is different.

First, we declare variable arrOne using let keyword and assign an javascript array to it which contain an single element only.

let arrOne = [8];

Now, in the second step create another variable arrTwo using let and assign a javascript array with single element.

let arrTwo = [9];

Finally, we are running javascript array comparison with direct value or array also.

  1. console.log(arrOne == 8); // output => ?
  2. console.log(arrTwo == 9); // output => ?
  3. console.log(arrOne < arrTwo); // output => ?

So, Any guess what should be the output of all above statement.

Answer – true, true, true

Click To Run Code

Explanation

Now, you know the answer is true, true, true. But why because it’s array and you are not comparing array value. Let me exlpain.

As it’s correct that array is different data type and number is different. And even if array compare with together they first converted during comparison.

Array Change In Comparison –

See, when javascript array is compared directly with any data type. Array first converted to string. So, if I convert it.

  1. console.log(“8” == 8); // output => true
  2. console.log(“9” == 9); // output => true
  3. console.log(“8” < “9”); // output => true

So, when equal is used then it only compare value not data type.

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

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 – object seal method can restrict value change
  2. JS Quiz – can string split method will be limited
  3. JS Quiz – can array from method be like a map method
  4. JavaScript Quiz – Infinity number is a true or false value

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