Talk:Core JavaScript 1.5 Reference:Global Objects:String:slice
From MDC
[edit] Negative zero in slice
Look at the following javascript code fragment.
str1 = "Sample Text."; str2 = str1.slice(-0); document.write(str2);
In most (maybe all) implementations the entire string is printed.
but...
In javascript there is a difference between 0 (zero) and -0 (negative zero). Because the first parameter is negative, the slice starts from the end of the string.
The result therefore must be an empty string.
Am i missing something?
--Jacco 08:23, 7 February 2007 (PST)
- The specification for ECMAScript (which is basically JavaScript minus a few extensions), in its description of slice for both Array and String objects, describes the process by which each method works in terms that make it clear that the passed-in number is treated as an integer, not as a floating-point number. (An early part of the spec says that methods which handle floating-point values specially will include explicit steps to deal with +/-0, +/-Infinity, NaN, etc. These methods include no such steps.) Consequently, -0 and +0 both map to the mathematical quantity 0, and the entire string is returned. --Waldo 15:26, 7 February 2007 (PST)