Answers for "slice function js"

63

javascript slice array

// array.slice(start, end)
const FRUITS = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = FRUITS.slice(1, 3);
// citrus => [ 'Orange', 'Lemon' ]

// Negative values slice in the opposite direction
var fromTheEnd = FRUITS.slice(-3, -1);
// fromTheEnd => [ 'Lemon', 'Apple' ]
Posted by: Guest on March-05-2020
39

javascript slice

//The slice() method extracts a section of a string and returns 
//it as a new string, without modifying the original string.

// same in array but you select elements not characters  


const str = 'The quick brown fox jumps over the lazy dog.';

console.log(str.slice(31));
// expected output: "the lazy dog."

console.log(str.slice(4, 19));
// expected output: "quick brown fox"

console.log(str.slice(-4));
// expected output: "dog."

console.log(str.slice(-9, -5));
// expected output: "lazy"

console.log(str.slice(0, 2)); 
// expected output: "the"
// Up to and including the last index!!!
// Different for python.
Posted by: Guest on March-22-2020
2

slice in javascript

var str1 = "Apple, Banana, Kiwi";

var res1 = str1.slice(7, 13);
var res2 = str1.slice(-12, -6);
var res3 = str1.slice(7);
var res4 = str1.slice(-12);
var res5 = str1.substring(7, 13); //substring() cannot accept negative indexes like slice.
var res6 = str1.substr(7, 9); // substr(start, length) is similar to slice(). second parameter of substr is length of string

document.write("<br>" + "slice:", res1);
document.write("<br>" + "slice -ve pos:", res2);
document.write("<br>" + "slice with only start pos:", res3);
document.write("<br>" + "slice with only -ve start pos", res4);
document.write("<br>" + "substring:", res5);
document.write("<br>" + "substr:", res6);
Posted by: Guest on June-04-2021
8

js slice

//The slice() method extracts a section of a string and returns 
//it as a new string, without modifying the original string.

// same in array but you select elements not characters  


const str = 'The quick brown fox jumps over the lazy dog.';

console.log(str.slice(31));
// expected output: "the lazy dog."

console.log(str.slice(4, 19));
// expected output: "quick brown fox"

console.log(str.slice(-4));
// expected output: "dog."

console.log(str.slice(-9, -5));
// expected output: "lazy"
Posted by: Guest on March-17-2020
3

js slice

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

const slicedArr = arr.slice(1,4); // slicedArr = [2,3,4]
Posted by: Guest on January-20-2021
3

array index javascript show only first 2 elements

array.slice(0, n);
Posted by: Guest on January-21-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language