This question already has answers here:
What does ~~ ("double tilde") do in Javascript?
(12 answers)
Closed 7 years ago.
I was just messing with random stuff, while I found something interesting..
if I have ~ before a number, for example I have tried
~110100100 // result will be " -110100101 "
~11 // result will be " -12 "
is it making it negative and reducing it by 1? I don't have any idea, can anyone pleas explain this??
The operator ~ returns that result:
~N = -(N+1)
But this is an effect of inverting the value of all bits of a variable.
Double tilde ~~ is used to convert some types to int, since ~ operator converts the value to a 32-bit int before inverting its bits. Thus:
~~'-1' = -1
~~true = 1
~~false = 0
~~5.6 = 5
Related
This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
What is the purpose of a plus symbol before a variable?
(4 answers)
Closed 4 years ago.
Can anyone tell me why and how the expression 1+ +"2"+3 in JavaScript results in 6 and that too is a number? I don't understand how introducing a single space in between the two + operators converts the string to a number.
Using +"2" casts the string value ("2") to a number, therefore the exrpession evaluates to 6 because it essentially evaluates to 1 + (+"2") + 3 which in turn evaluates to 1 + 2 + 3.
console.log(1 + +"2" + 3);
console.log(typeof "2");
console.log(typeof(+"2"));
If you do not space the two + symbols apart, they are parsed as the ++ (increment value) operator.
It's simple first it convert the string +"2" to number(According to the operator precedence) and then add all these.
For Operator precedence mozilla developer link
1+ +"2"+3 results 6
1+"2"+3 results "123"
AS The unary + operator converts its operand to Number type.
+"2" is a way to cast the string "2" to the number 2. The remain is a simple addition.
The space between the two + operators is needed to avoid the confusion with the (pre/post)-increment operator ++.
Note that the cast is done before the addition because the unary operator + has a priority greater than the addition operator. See this table: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table
This question already has answers here:
What does the "|" (single pipe) do in JavaScript?
(5 answers)
Closed 4 years ago.
I'm wondering how JavaScript evaluates the following expression:
10.333333 | 0 === 10
Is it because of bitwise ORing ignores the decimal part?
JavaScript bitwise operators all work by converting their operands to 32-bit integers. The operation is performed and the result is converted back to a (floating point) number.
This question already has answers here:
Why does JavaScript handle the plus and minus operators between strings and numbers differently?
(7 answers)
Adding and subtracting strings and numbers in Javascript - auto type conversion?
(5 answers)
Closed 5 years ago.
What will be an output of:
var number = "1.2";
console.log(number - 0.2);
console.log(number + 0.2);
And why?
The answer will be
1 and
1.20.2 respectively
Note that number is string, but since - operator is not supported by string, JS converts it to number thus the output 1. for the second case, since + operator is supported by string, it will simply concatenate it, hence the answer 1.20.2
The output is
1
1.20.2
Why ?
In the first case the variable string is converted to number because there is no - operator for strings
But there is a + operator for string and it make a concatenation of string, it is in the second case prefered to first convert into number
This question already has answers here:
Using bitwise OR 0 to floor a number
(7 answers)
Closed 8 years ago.
Please describe how does 5.55 | 0 produces 5 in JavaScript. I want to know what is happening in this bitwise operating. Thanks!
The bitwise operators in Javascript automatically coerce their arguments to 32-bit integer values by dropping the fraction and any high-order bits beyond 32. So
5.55 | 0
is treated like:
5 | 0
The operands of bitwise operations are always converted to signed 32-bit integers in big-endian order and in two's complement format.
That would be
00000000000000000000000000000101
or 00000000000000000000000000000000
------------------------------------
00000000000000000000000000000101
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to: The ~ operator?
What's the ~ operator does in javascript?
input
alert(~1)
output is
-2
input
~function () {}()
output is
-1
I never heard about ~ operator in javascript
https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators
Bitwise NOT ~ a Inverts the bits of its operand.
I guess its fairly odd that a function returns -1, but what would you expect anyway.
This is the bitwise not operator that inverts the value of every bit in the integer. In binary a signed integer has the following representation:
00000001 = 1
11111110 = -2
See this wikipedia article.
The bitwise NOT operator (~) will take its operand, convert it to a
32-bit integer, and will invert each bit so that each 0 becomes a 1
and vice versa.
http://james.padolsey.com/javascript/double-bitwise-not/