What is the difference between non-initialized items in an array in JavaScript vs Java?

后端 未结 3 1434
长情又很酷
长情又很酷 2021-01-07 19:13

I was just playing around with JavaScript and got stuck with a simple program.

I declared an array in JavaScript like

var a = [0, 1, 2];
3条回答
  •  不要未来只要你来
    2021-01-07 19:39

    An array is a continuous collection of data. Say if you have a value at index 1 and index 10, the rest will be filled by undefined.

    You can't create an array with empty values. 'Empty in your sense', is not undefined or null, it's empty :)

    That continuous data is undefined means default assignment in JavaScript.

    Say if you defined a variable,

    var X;
    console.log(X)    //Undefined
    

    it's not null. null is a user explicitly specifying a way to tell there is an empty data, and undefined is the JavaScript engine way.

提交回复
热议问题