Home »
MCQs »
JavaScript MCQs
What is the output of the following JavaScript code? | Question 13
62. What will be the output of the following JavaScript code?
<script>
let cars1 = ['Honda', 'Hyundai'];
let cars2 = cars1;
cars1.push('Mahinda');
document.write(cars1 + "---" + cars2);
</script>
- Honda,Hyundai,Mahinda---Honda,Hyundai
- Honda,Hyundai,Mahinda---Honda,Hyundai,Mahinda
- Honda,Hyundai ---Honda,Hyundai
- [Honda,Hyundai,Mahinda]---[Honda,Hyundai,Mahinda]
Answer: B) Honda,Hyundai,Mahinda---Honda,Hyundai,Mahinda
Explanation:
In the JavaScript, the arrays are objects, and the array elements are stored by reference. Hence, when an array value is copied, any change in the copied array will also reflect in the original array. Thus, the values of cars1 and cars2 are the same.