JavaScript Trick:1
Part-1

JavaScript Trick:1

#javascriptInputOutput

let arr=[1,2,3,4,5];
arr.length=0;
console.log(arr);        

Look it carefully , there has an array (arr) with five elements(1,2,3,4,5) . So, initially array length is 5. Now I makes array length to zero using inbuild length function of JavaScript and trying to console. what will be the output ?

The output should be blank array, because I have assigned the array length 0. The array converts into a blank array or empty array.

Now what will happen ?

let arr=[1,2,3,4,5];
arr.length=8;
console.log(arr[6]);
console.log(arr);
        

Again I am trying to assign array length 8 using JavaScript inbuild length function. Now trying to console 6th element of this array and whole array . what will be output now ?

The output should be [1, 2, 3, 4, 5, empty × 3]. Three empty element are added in the array. Now array will be [1,2,3,4,5,,,]. So, the first output should be undefined . because 6th cell is empty.

要查看或添加评论,请登录

Abhisek Mati的更多文章

  • #javascriptInputOutput

    #javascriptInputOutput

    What will be the output ? (function(){ ? ? var a=5; ? ? ?var b=5; })(); console.log(b); Output will be an error (b is…

  • #javascriptInputOutput

    #javascriptInputOutput

    to empty an array in JavaScript? let var arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; Method 1:- arrayList = []; This…

社区洞察

其他会员也浏览了