Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript slice and substring

string.slice(start, stop);
string.substring(start, stop);

What they have in common:

-If start equals stop: returns an empty string
-If stop is omitted: extracts characters to the end of the string
-If either argument is greater than the string's length, the string's length 
will be used instead.
    
Distinctions of substring():

-If start > stop, then substring will swap those 2 arguments.
-If either argument is negative or is NaN, it is treated as if it were 0.

Distinctions of slice():

-If start > stop, slice() will return the empty string. ("")
-If start is negative: sets char from the end of string.
-If stop is negative: sets stop to: string.length – Math.abs(stop) 
(original value), except bounded at 0.   
Comment

Substring in Javascript using slice

let str = "Learning to code";

// slice between index 0 and index 5
console.log(str.slice(0, 5));
// slice between index 5 and index 10
console.log(str.slice(5, 10));
Comment

javascript string slice

let x = ["a", "b", "c", "d", "e", "f"];

console.log(Array.prototype.slice.call("abcdefg"))
/*[ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ]*/
/*slice can turn a string into an array with its letters as elements*/
Comment

PREVIOUS NEXT
Code Example
Javascript :: js replace text link with anchor tags 
Javascript :: slideshow react npm 
Javascript :: how to give data from react native to webview 
Javascript :: sort array of objects based on another array javascript 
Javascript :: socket io express 
Javascript :: show ad on facebook game 
Javascript :: how to check with jquery if bootstrap modal is hidden or shown 
Javascript :: leaflet flyto 
Javascript :: javascript ean13 checksum generate 
Javascript :: How to have hotjar in react-hotjar 
Javascript :: accept json data in express 
Javascript :: reactjs sweet alert 
Javascript :: leaflet js mobile popup not opening 
Javascript :: search an array with regex javascript find 
Javascript :: how to link prototypes in js using call method 
Javascript :: query selector 
Javascript :: js modulo not working 
Javascript :: select div with clas 
Javascript :: load images js context 
Javascript :: append array to array javascript 
Javascript :: js two array combining with id neasted 
Javascript :: display text on button click react 
Javascript :: script defer attribute 
Javascript :: get parent class javascript 
Javascript :: javascript get user from api 
Javascript :: regex match between quotes without escape 
Javascript :: camel case first javascript 
Javascript :: js fit window to content 
Javascript :: adding cors parameters to extjs ajax 
Javascript :: javascript redirect to file 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =