On the point solutions to your coding problems

  • How to Empty an Array in JavaScript

    Consider this JavaScript array:

    var arr = [1,2,3,4,5];

    To empty this array, you can use one of the following methods:

    Method 1:

    Reassign the array to an empty array. Here is how to do it.

    arr = []; // reassign array to empty one

    You should use this approach only when there is no reference attached to the existing array. This is also the fastest approach to emptry an array in JavaScript.

    Method 2:

    Reset the length of an array to 0. Here is how to do it.

    arr.length = 0;

    Method 3:

    Using splice() function.

    Here is how to do it.

    arr.splice(0,arr.length);

    This approach will actually return an original array after removing each item from position 0 of an array to the last index.

     

  • How to Reverse a JavaScript Array

    You can use the native reverse() function to reverse an array in JavaScript.

    Here is the sample code.

    var list = [1, 2, 3, 4, 5];
    list.reverse();
    // 5,4,3,2,1

    You can store the array in a separate variable using the slice() operator.

    var list = [1, 2, 3, 4, 5];
    var reversedList = list.slice().reverse();
    console.log(list);
    // 1,2,3,4,5
    console.log(reversedList);
    // 5,4,3,2,1

     

  • How to install VSCode in Ubuntu and CentOS Linux

    To install VSCode in Ubuntu, follow the steps mentioned below:

    Step 1: Download the installer

    Navigate to the official VSCode website and download the installer. Click here to navigate to the download page.

    Step 2: Run the Installer

    If you are using the latest Ubuntu or Debian Linux distribution, then run the following command to install VSCode.

    sudo apt install ./<location_to_downloaded_file>.deb

    If you are using the old version of Ubuntu or Debian Linux distribution, then run the following command.

    sudo dpkg -i <location_to_downloaded_file>.deb
    sudo apt-get install -f

    If you are using CentOS-based distribution Linux, you can use the following command to install VSCode.

    sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
    sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo'

    Now, you can run the VSCode in your system and start coding.

     

  • Clone Arrays in JavaScript

    You can use the following code to clone arrays in JavaScript.

    var arr =  [ 10, 20, 30, 4, 5, 6, 112, 333, 4, 2, 5, 66 ]
    
    var arr2 = arr.slice(0);
    
    console.log('Actual array ', arr);
    console.log('Cloned array', arr2);