What is the output of the below javascript code? <script…

2013

What is the output of the below javascript code?

<script type="text/javascript"
language="javascript"> var qpt = new Array(); qpt[0] = "WebDevelopment"; qpt[1] = "ApplicationDevelopment"; qpt[2] = "Testing"; qpt[3] = "QualityPointTechnologies"; document.write(qpt[0, 1, 2, 3]) ; </script>

  1. A.

    Error

  2. B.

    QualityPointTechnologies

  3. C.

    WebDevelopment

  4. D.

    Web Development, Application Development, Testing, Quality Point Technologies

Attempted by 27 students.

Show answer & explanation

Correct answer: B

Concept

In JavaScript the comma operator evaluates each of its comma-separated operands from left to right and returns the value of the last operand only. So an expression like (a, b, c) discards a and b and yields c. This is unrelated to array literals — inside a subscript [...] the contents are still just one expression, so a single index is computed.

Application

  1. The array holds four strings at indices 0..3, with index 3 set to QualityPointTechnologies.

  2. The subscript qpt[0, 1, 2, 3] contains the comma expression 0, 1, 2, 3. The comma operator evaluates 0, then 1, then 2, then keeps only the last value, 3.

  3. So the subscript reduces to qpt[3], and document.write prints QualityPointTechnologies.

Cross-check

Evaluating (0, 1, 2, 3) on its own in a console returns 3, confirming the bracket resolves to index 3. The brackets never produce an array or an error, and only one element is ever indexed — so the whole list is not printed and no syntax error occurs.

Explore the full course: Btsc Lab Assistant