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.
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] 삭제
몇 마디 만하겠습니다