Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Slicing an Array

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 117
    Comment on it

    Basically slice() method is used to extract some particular elements from an original array (suppose i need to show only selected elements from original array). It contain two parameter start/begin and end.
    begin ->It is the position where to start the selection .And if you want to select element from end then you can use negative number like (1 , -3) .
    Note -> If we omit the begin parameter, then slice start from index 0.

    end -> It is the position where to end the selection .Like slice(1,5) , then it extract the second elements upto the third elements(element inded is 1, 2, 3, 4 ).
    Note -> If we omit the end parameter, then slice will extracts to the end of the sequence.

    Syntax ->
    arrayname.slice(begin/start , end);
    Example ->

    var arr = ['Mukesh' , 'Ravi' , 'Ayush' , 'Pranav' ] ;<br>
    var newarr = arr.slice(0 , 2);<br>
    document.write(newarr);<br>
    

    Output -> ['Mukesh' , 'Ravi']
    Note -> For object reference ,slice will copy object reference to new array but both the array will refer to the same object .It means when we do any change in original array then it also reflects in new array also.But in case of strings and numbers slice will copies strings and number into the new array it means when we do any change in an array then it will not reflect to other array .

    Note In Javascript there is also one method , name is little bit similar to slice() method , is splice() method.
    splice() -> This method is used to change the content of an array by removing current elements and add new elements .So don't confuse with it

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: