Home > Web Front-end > JS Tutorial > body text

What is the difference between String.slice and String.substring in js

coldplay.xixi
Release: 2020-11-30 15:46:16
Original
2552 people have browsed it

The difference between String.slice and String.substring in js is: 1. If [start>stop], then substring will exchange these two parameters; 2. If [start>stop], slice will return a null character string.

What is the difference between String.slice and String.substring in js

The difference between String.slice and String.substring in js is:

slice() is similar to substring( ) , but has some different behavior.

Syntax: string.slice(start, stop);
Copy after login
Syntax: string.substring(start, stop);
Copy after login

What do they have in common:

  • If start equals stop: Return an empty string

  • If stop is omitted: extract characters to the end of the string

  • If either parameter is greater than the length of the string, the length of the string will be used instead.

substring() Difference:

  • If start > stop, then substring will exchange these two parameters.

  • If either argument is negative or NaN, it is treated as 0.

slice() Difference:

  • ##If start > stop, slice() will return an empty string. ( "" )

  • If start is negative: Sets char from the end of the string, just like substr() in Firefox. This behavior is observed in both Firefox and IE.

  • If stop is negative: set stop to: string.length – Math.abs(stop) (original value), but in range 0 in the ECMA specification (so, Math .max(0, string.length stop) ) is the limit.

Related free learning recommendations: JavaScript(Video)

The above is the detailed content of What is the difference between String.slice and String.substring in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template