What is the use of "or" operator with integers [duplicate] - javascript

This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 6 years ago.
I am not getting what or operator does with inters. I have following code
-1||4 // output -1
4||-1 //output 4
Does it converts integers in bytes and performs or operation.

It first checks wheter the number is truthy or falsey and returns the first truthy one. All numbers are truthy except for 0.
0 || 4; // 4
2 || 3; // 2 (picks the first one, because both true)
-3 || 0; // -3
0 || -2; // -2
Does it converts integers in bytes and performs or operation?
No. The || operator is logical and, not bitwise and.

Related

What is the difference and what are the "&" and "&&" operators used for? [duplicate]

This question already has answers here:
What's the difference between & and && in JavaScript?
(4 answers)
Closed 3 years ago.
I have two conditions that validate if "a" equals zero and "b" less than 5, both behave in the same way, but with different operators, is there any real difference between them?
var a = 0;
var b = 3;
if (!a & (b < 5)) {
}
if (!a && b < 5) {
}
The key difference is that the & operator is a bitwise operator, while the && operator is a logical operator.
Bitwise operators work on bits and perform "bit by bit" operations, they are applied to the bits of one or two operands. The & represents a bitwise AND operation- where A and B represent two inputs; both inputs must be true in order for C to be true.
So for instance in the example you provided you have:
if(0 & 1){
}
The result of the bitwise AND on 0 and 1 as inputs is 0 (false) because both inputs must be true for the output to be true.
The && operator is a logical operator, which is used to make a decision based on multiple conditions. It can apply to one or two operands each of which may be true or false.
if (!a && b < 5) {
}
The above still evaluates to false because both conditions must be met for the code inside the if statement to be executed. In addition to this, using the && operator will (depending on the language) short circuit, which means that the second condition of the if will only be evaluated if the outcome is not determined by the first condition.
So this expression will fail as soon as a is found to be zero (false) because there is no point in evaluating the second expression as the first expression had to be true. This is another difference as the bitwise & operator does not short circuit.

Why 0 and null are true and false with equal operator? [duplicate]

This question already has answers here:
Why `null >= 0 && null <= 0` but not `null == 0`?
(6 answers)
JavaScript comparison operators: Identity vs. Equality
(4 answers)
Closed 6 years ago.
I want to ask about a weird javascript thing. All of these conditions, in my opinion, contradict each other and return false:
0 > null
0 < null
0 == null
0 === null
Why does using >= and <= operators return true? >= means gt and <= means lt. They couldn’t be equals. Moreover, “null” has a null value, 0 has a null value and, for the logic 0 > null should return true. Could someone explain me this fact?
When you use > and <, null is converted to the number 0. 0 > 0 and 0 < 0 are both false (that's basic math). When you use == and ===, null is not converted. 0 is not equal to null and hence both are false as well.
More generally speaking: Operators are defined for specific data types and if you pass a value of a different data type that value will be converted to the expected data type first. > and < are defined for strings and numbers but not for null. Hence null is (eventually) converted to a number.
== are a little different ===. While == usually performs type conversion, it doesn't do that if you compare against null. That's simply how the algorithm works.

| vs || ... Does it matter which one I use? [duplicate]

This question already has answers here:
Javascript conditional statement with a single pipe "|"
(4 answers)
Closed 7 years ago.
Does is matter whether I use | or || as an or operator? Both seem to work interchangeably.
function Sum(num, sum) {
sum = sum | 0;
return sum;
}
function SumII(num, sum) {
sum = sum || 0;
return sum;
}
console.log(Sum(7));
//0
console.log(SumII(7));
//0
Yes, there is a huge difference.
The operator | is the bitwise operator or.
I cite the linked documentation:
Returns a one in each bit position for which the corresponding bits of either or both operands are ones.
So, by using this operator with two numbers will give you another number that is built from the first twos.
The operator || is the logical operator or.
It evaluates its operands as booleans (imagine as an implicit cast take place if needed) and it returns the first true value in its original form.
It happens that for certain operations the result is the same (as an example, if used in a guard, where the result of the applied operator is treated as a boolean value), no matter which operator you decide to use, but it would be better to know what are the differences between them.
Logical operations
Use || and && instead of | and & because the first ones use a "short circuit" mechanism. So, they are more efficient (and safe) because if the left term was already evaluated to true / false, the result is known without evaluating the last terms.
E.g.: (for an undefined x)
true | x // ReferenceError: x is not defined
true || x // true
false & x // ReferenceError: x is not defined
false && x // false
Bits operations
Use | and &.
E.g.:
4|2 returns 6
4 = 100b
2 = 010b
6 = 110b
(see the OR operation on every column: 1 OR 0 = 1, 0 OR 1 = 1, 0 OR 0 = 0)
4||2 returns 4
4 is a "true" value (because it is not 0,false,null,"", undefined or NaN) => the result doesn't depend on the bits values => the operator is not bitwise
They are not the same
| is BITWISE OR operator. It takes the binary value of the two operands and compares each bit using OR logic.
|| is the LOGICAL OR operator. It checks the truthy-ness of each operand and compares those using OR logic.
Try for example
x = 42 || 65 // X now equals 42
x = 42 | 65 // X now equals 107 - 42 bitwise ORed to 65

In JavaScript what's the difference between Equal to ('==' & '===') [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 7 years ago.
I am trying to understand equality in JavaScript. Here is the code.
var x = prompt("What is 10 + 10");
if (x === 10) {
document.write("Correct")
}
else {
document.write("Incorrect")
}
Why wouldn't I make the equals sign like "===". So if "10" is equal("===") to "x"(user answer) then it should be correct right?
I searched on both Stack Overflow and W3Schools, but couldn't find what I was looking for. I guess I'm just nor getting this "true or false" thing. I mean this seems like a very simple equation. Help would be great thanks guys!
=== is strict type equality which compares by both value and type
== is non-strict type equality, which compares only by value.
In other words, == performs type conversion and then compares values for equality. Here are some examples
"3" == 3
=> true
Explanation: The string 3 is converted to the number 3, which is equal to 3.
"3" === 3
=> false
Explanation: The string is not converted to a number. Thus the string 3 does not equal the number 3.
In your example, incorrect would be written to the document. That is because the result of prompt returns a string, and you are performing strict equality with a number.
In your case, the interpreter sees it like this
if ("10" === 10) {
// does the string "10" equal the number 10? If so
document.write("Correct")
}
else {
// Hey, wait a minute. It doesn't equal the number. I should write "Incorrect" instead.
document.write("Incorrect")
}
In Javascript,
== means: is equivalent to
=== means: is identical to
When the value of x is "10", x is equivalent to 10.
But it isn't identical to 10.

Values comparison [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 8 years ago.
[4] === 4 // is: false
[4] == 4 // is: true
'0' == 0 // is: true
'0' === 0 // is: false
Can anyone give the exact reason for this?Also what exactly does strict equality operator do or need for comparision?I learned that type and value should be same for strict(===) operator.Is this what strict equality operator checks .If yes,than how equl to operator works?
== Compare values
=== Compare values and type
For example
[4] //turns into "4" when comparing
"4" == 4 //They are the same
"4" === 4 //The values are the same, but not the type
Reference: http://es5.github.io/#x11.9.4
http://i.stack.imgur.com/q13LO.png
The === operator also compares the type of the object.
So, in [4] === 4
[4] is an array, but 4 is a number, so that evaluates to false.
And in '0' === 0
'0' is a string, but 0 is a number, so that evaluates to false.
The === operator compares both type and value, making it a stricter check.
The == operator performs a less strict value based checked. It will in some cases consider values of different types "equal" Such examples are 0 vs '' or 0 vs "0"
The == operator will see 0 and '' as equal whereas the === operator will not treat them as equal since they are of different types.

Categories