Possible Duplicate:
What is the “double tilde” (~~) operator in JavaScript?
The D3 tutorial gives a function that produces a random sequence:
var t = 1297110663, // start time (seconds since epoch)
v = 70, // start value (subscribers)
data = d3.range(33).map(next); // starting dataset
function next() {
return {
time: ++t,
value: v = ~~Math.max(10, Math.min(90, v + 10 * (Math.random() - .5)))
};
}
Note the ~~ (tilda tilda) in:
value: v = ~~Math.max(10, Math.min(90, v + 10 * (Math.random() - .5)))
From playing around in the javascript terminal, I see:
~~1
1
~~-1
-1
~~-1.3
-1
parseInt(5)
5
parseInt(-5)
-5
parseInt(-5.3)
-5
parseInt(5.3)
5
Since ~~ and parseInt seem to be equivalent, whats the rationale for using parseInt?
Hence for converting some non-numeric value to number we should always use Number() function. eg. There are various corner case to parseInt() functions as it does redix conversion, hence we should avoid using parseInt() function for coersion purposes.
parseFloat( ) parseFloat() is quite similar to parseInt() , with two main differences. First, unlike parseInt() , parseFloat() does not take a radix as an argument. This means that string must represent a floating-point number in decimal form (radix 10), not octal (radix 8) or hexadecimal (radix 6).
parseInt is for converting a non integer number to an int and parseFloat is for converting a non float (with out a decimal) to a float (with a decimal). If your were to get input from a user and it comes in as a string you can use the parse method to convert it to a number that you can perform calculations on.
They are not equivalent.
parseInt()
turns strings into numbers, reading up to and ignoring the first non-integer character, and also possibly performing base conversion (e.g. interpreting a string as octal, or hexadecimal).console.log(
parseInt(011) +'\n'+ // 9
parseInt('011') +'\n'+ // 11
parseInt('42 cats') +'\n'+ // 42
parseInt('0xcafebabe') +'\n'+ // 3405691582
parseInt('deadbeef',16) +'\n'+ // 3735928559
parseInt('deadbeef',36) +'\n' // 1049836114599
);
var x = ~~y;
is a 'trick'—similar to var x = y << 0;
—that (ab)uses the unary bitwise NOT operator to force the result to be in the range of a signed 32-bit integer, discarding any non-integer portion.console.log(
parseInt(011) +'\n'+// 9
parseInt('011') +'\n'+// 11
parseInt('42 cats') +'\n'+// 42
parseInt('0xcafebabe') +'\n'+// 3405691582
parseInt('deadbeef',16) +'\n'+// 3735928559
parseInt('deadbeef',36) +'\n' // 1049836114599
);
Using ~~x
is often done because:
As seen in the cafebabe
test, numbers above 231 -1 ( 2,147,483,647 ) or below -231
( −2,147,483,648 ) will "wrap around" due to the limits of a signed 32-bit integer.
parseInt
isn't limited to signed 32 bit numbers.
// Top range for a bitwise operator provides a valid result
~~((Math.pow(2,32)/2)-1); // 2147483647
// Beyond the top range provides undesired result
~~(Math.pow(2,32)/2); // -2147483648
Also, with parseInt
you can specify the radix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With