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

PREVIOUS NEXT
Code Example
Javascript :: splice method in javascript 
Javascript :: regex match first result only 
Javascript :: trim string 
Javascript :: in vs of javascript 
Javascript :: regex char or char 
Javascript :: how the concat function works javascript 
Javascript :: one component to another component in vuejs trigger function 
Javascript :: javascript date validation less than today 
Javascript :: express prisma 
Javascript :: adding a if stement in jsx 
Javascript :: maximum sum array algorithm javascript 
Javascript :: vscode add shortcut to run in terminal 
Javascript :: jquery validation from data 
Javascript :: js overflowy 
Javascript :: points in three js 
Javascript :: video conferencing app with html and js 
Javascript :: stripe angular 
Javascript :: regex date checker 
Javascript :: best reactjs course on udemy 
Javascript :: nginx reverse proxy redirect 
Javascript :: js get first and last day of previous month 
Javascript :: rich text react renderer 
Javascript :: tobe a number jest 
Javascript :: javascript on screen width change 
Javascript :: how to print two arrays together 
Javascript :: max method in js 
Javascript :: adding commas after thousand 
Javascript :: counter in html and js 
Javascript :: how to run cypress test 
Javascript :: mongoose bulk update 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =