Nodejs Array Slice slice(index = 0)

Here you can find the source of slice(index = 0)

Method Source Code

Array.prototype.slice = function(index = 0) {
   var arr = [];/*from  w ww  . j a  v  a2s.  com*/
   var length = this.length;

   if ( typeof index != "number" )
      throw 'TypeError';

   if ( length <= 0 )
      length = 0;

   for (index; index < length; index++)
      arr.push(this[index]);

   return arr;
}

Related

  1. slice(index)
    Array.prototype.slice = function(index) {
      result = []
      if (typeof index != "number") {
        for (index; index < this.length; index++) {
          result.push(this[index])
      return result
    
  2. slice(start, end)
    Array.prototype.slice = function(start, end) {
      if (end == null) {
        end = this.length;
      if (start < 0) {
        start = this.length + start;
      if (end < 0) {
        end = this.length + end;
    ...
    
  3. slice(start, end)
    Array.prototype.slice = function(start, end) {
        var arr = [];
        if (start === undefined && end === undefined) {
            for (var i=this.length;i--;arr.unshift(this[i]));        
        } else if (end === undefined) {
            for (var i = start; i < this.length; ++i) { arr.push(this[i]) }
        } else {
            for (var i = start; i < end && i < this.length; ++i) { arr.push(this[i]) }
        return arr;
    };
    
  4. sliceMe(begin, end)
    Array.prototype.sliceMe = function (begin, end) {
      if (arguments.length < 1) {
        begin = 0;
        end = this.length;
      } else if (arguments.length < 2) {
        end = this.length;