JavaScript Example (Program)
Learn Details

Q. Where do use reverse() method ?

जिस array के element को reverse (मतलब left side से right side की तरफ ) करना हो , तब reverse method का use करते हैं

ArrayName.reverse()
  • Example



    <script>
       // String value print
    var a = ["Cat", "Apple", "Ball", "Dog"];
    document.write( "<b>Before Reverse Method:  </b>"+a+"<br>")
    a.sort();
    a.reverse()
    document.write( "<b>After Reverse Method:  </b>"+a);
       
    </script>
    Out Put : Before Sort Method : Cat,Apple,Ball,Dog
    After Sort Method : Dog,Cat,Ball,Apple
    <script>
    // Number value print
    var a = [10, 100, 2, 20, 3, 30];
    document.write("<strong>Normal Value : </strong>"+a+"<br>")
    function comp(a,b){
    return a-b;
    }
    a.sort(comp);
    a.reverse()
    document.write("<strong>reverse Value : </strong>"+a)
    
    </script>
    Out Put : Normal Value : 10,100,2,20,3,30
    reverse Value : 100,30,20,10,3,2