All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Array Remove First Element

Last Updated : Mar 11, 2024

JavaScript Array Remove First Element

In this article we will show you the solution of JavaScript array remove first element, we are going to use JavaScript to delete the first element from an array. we will be using two different methods in order to do so.

  • Using ‘for loop’ : in order to do deleting any element from the array we will use for loop. Let us see the syntax of the for loop first, for (initialization ; condition ; increment/decrement) ;
  • Using shift() : the shift() property of JavaScript is used to delete the last element from an array.

Step By Step Guide On JavaScript Array Remove First Element :-

Method 1 - Using For Loop

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> javascript array remove first element </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        h1 {
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
    </style>
</head>
<body>
    <center>
    <h1> WELCOME TO TALKERSCODE </h1>
    <h3> javascript array remove first element </h3>
    </center>
    <script>
      let array = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] ;
      array.shift() ;
      document.write(array) ;
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> to mention the version of the HTML file
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Attach an external CSS file using <link> tag
  5. <h1> tag used to add heading here and also adding the inline CSS here.
  6. Create a <Script> tag to write JavaScript within it.
  7. Now create an array with the values
  8. Using array.shift() method to remove the first element
  9. Document.write() to display the array after removal of the first element

Method 2 - Using Shift() Method

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> javascript array remove first element </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
    <style>
        h1 {
          color : rgb(95, 194, 95) ;
          font-size: 25px;
          font-weight : bolder ;
        }
    </style>
</head>
<body>
    <center>
    <h1> WELCOME TO TALKERSCODE </h1>
    <h3> javascript array remove first element </h3>
    </center>
    <script>
        let obj = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] ;
        for(let i=0; i<obj.length-1; i++) {
            obj[i] = obj[i+1] ;
        }
        obj.length = obj.length-1 ;
        document.write(obj) ;
    </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> to mention the version of the HTML file
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Attach an external CSS file using <link> tag
  5. <h1> tag used to add heading here and also adding the inline CSS here.
  6. Create a <Script> tag to write Javascript within it.
  7. Using Let to create an array name obj with some values.
  8. Create a for loop. Set the value of i=0 and i < obj.length-1.
  9. within it set the array position to the next position
  10. Now set the array length to be the same as before
  11. Using document.write() to display the array after removing the element

Conclusion :-

At last, here in conclusion, we can say that with this article’s help, we know how to remove first element from array using JavaScript.

I hope this article on JavaScript array remove first element helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪