Why two different methods slice() & substring()?

Somnath Kharat
var name="nameSomnath";//remove name

I can do with slice()

var result = name.slice( 4 );

Same can be done with substring()

var result = name.substring( 4 );

So what makes them different. I have seen the link Here which elaborates the difference .But we can do the same thing by using any one method ie slice() or substring().So why there was need to have two methods.

Gabe

Even though it looks superficially like slice and substring do the same thing, the big difference is in how they handle negative arguments.

When JavaScript was first created in Netscape 2.0, there was just a substring method. If either of its arguments are negative, they are treated as 0.

When JavaScript 1.2 was introduced with Netscape 4.0, they wanted to add the behavior of allowing negative indexes to mean distances from the end of the string. They couldn't change substring to have this new behavior because it would break backward compatibility with scripts that expected negative indexes to be treated as 0, so they had to create a new function to support the added feature. This function was called slice, and was implemented on Array as well as String.

Another, smaller difference is that with substring the order of the arguments doesn't matter, so substring(1, 4) is the same as substring(4, 1). With slice, order does matter, so slice(4, 1) will just yield an empty string.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why are these two Python unicode strings different?

분류에서Dev

Why do val and def implement abstract methods at different times?

분류에서Dev

Common Substring of two strings

분류에서Dev

What is the difference between these two type annotation and why they are different?

분류에서Dev

mean of non zero elements - why are these two attempt returning different results?

분류에서Dev

JAVA: Different objects with different methods

분류에서Dev

Different methods of concatenation return different output

분류에서Dev

왜 두 가지 다른 방법 인 slice ()와 substring ()?

분류에서Dev

Understanding & setting up different input methods

분류에서Dev

Java - Separating Class Methods into Different Files

분류에서Dev

Is there a different way to use methods of another class?

분류에서Dev

How to apply different sorting methods to different columns in a CSV file in BASH?

분류에서Dev

Add two values from two different Lists

분류에서Dev

Gap Between two two dates of different cells

분류에서Dev

Get a Substring in SQL between two characters and remove all white spaces

분류에서Dev

why would you slice an array from beginning to end?

분류에서Dev

Use a struct or create two methods? C#

분류에서Dev

Difference between these two "methods" for simple character encryption

분류에서Dev

How to install two different compilers?

분류에서Dev

synchronized hover of two different elements

분류에서Dev

combine two different queries into one

분류에서Dev

Why are top level methods handled differently in irb?

분류에서Dev

Why are static variables and methods in java called directly?

분류에서Dev

WebAPI: Why do these methods match the routing template?

분류에서Dev

Why is it said that static methods cannot be inherited?

분류에서Dev

why $PATH in vim and in zsh is different?

분류에서Dev

Set and Shopt-Why Two?

분류에서Dev

In EmberJS built by EmberCLI, how to call methods on different controllers?

분류에서Dev

Can't control Threads from different Methods WPF C#

Related 관련 기사

  1. 1

    Why are these two Python unicode strings different?

  2. 2

    Why do val and def implement abstract methods at different times?

  3. 3

    Common Substring of two strings

  4. 4

    What is the difference between these two type annotation and why they are different?

  5. 5

    mean of non zero elements - why are these two attempt returning different results?

  6. 6

    JAVA: Different objects with different methods

  7. 7

    Different methods of concatenation return different output

  8. 8

    왜 두 가지 다른 방법 인 slice ()와 substring ()?

  9. 9

    Understanding & setting up different input methods

  10. 10

    Java - Separating Class Methods into Different Files

  11. 11

    Is there a different way to use methods of another class?

  12. 12

    How to apply different sorting methods to different columns in a CSV file in BASH?

  13. 13

    Add two values from two different Lists

  14. 14

    Gap Between two two dates of different cells

  15. 15

    Get a Substring in SQL between two characters and remove all white spaces

  16. 16

    why would you slice an array from beginning to end?

  17. 17

    Use a struct or create two methods? C#

  18. 18

    Difference between these two "methods" for simple character encryption

  19. 19

    How to install two different compilers?

  20. 20

    synchronized hover of two different elements

  21. 21

    combine two different queries into one

  22. 22

    Why are top level methods handled differently in irb?

  23. 23

    Why are static variables and methods in java called directly?

  24. 24

    WebAPI: Why do these methods match the routing template?

  25. 25

    Why is it said that static methods cannot be inherited?

  26. 26

    why $PATH in vim and in zsh is different?

  27. 27

    Set and Shopt-Why Two?

  28. 28

    In EmberJS built by EmberCLI, how to call methods on different controllers?

  29. 29

    Can't control Threads from different Methods WPF C#

뜨겁다태그

보관