Sunday, 9 December 2018

JavaScript: How the extra commas are interpreted in Array Literal notation

Given below array, which has two commas with no data, how it is interpreted.
var arr = ["Cricket", , "Football"]

Missing elements in the array are treated as undefined.

HelloWorld.js
var arr = ["Cricket", , "Football"]

console.log("arr[0] : " + arr[0]);
console.log("arr[1] : " + arr[1]);
console.log("arr[2] : " + arr[2]);

Output
arr[0] : Cricket
arr[1] : undefined
arr[2] : Football


Previous                                                 Next                                                 Home

No comments:

Post a Comment