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>
  1. Honda,Hyundai,Mahinda---Honda,Hyundai
  2. Honda,Hyundai,Mahinda---Honda,Hyundai,Mahinda
  3. Honda,Hyundai ---Honda,Hyundai
  4. [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.

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.