I have created the following function in JavaScript which is working as expected:
<script>
function add(a, b, c) {
document.write('A: ' + a + '<br>');
document.write('B: ' + b + '<br>');
document.write('C: ' + c + '<br>');
}
add(10, 20);
</script>
Output:
A: 10
B: 20
C: undefined
However, the following function in JavaScript is not working as expected wherein it is not displaying undefined
value
<script>
function addArray(a = [101]) {
document.write('A: ' + a[0] + '<br>');
document.write('B: ' + a + '<br>');
document.write('C: ' + c + '<br>');
}
addArray([110]);
</script>
Output:
A: 110
B: 110
As per my understanding the output should be as below:
Expected Output:
A: 110
B: 110
C: undefined
Also, what is difference between a[0]
and a
in the above example?