I'm having a hard time understanding the difference between the comparison and logical "not" operators in Javascript. And I am confused about the syntax as well. My questions are:
Since they are both boolean operators, are there any real differences between the two?
And is the syntax for both like this?
x! = 5
Any explanation appreciated - please post examples if you can.
Comparison: take two values and compare them. We could ask various questions, for example:
are these two values "the same", we use == for that
is this value bigger than that value, >
is this value bigger than or equal to that, >=
The result of each of this is a boolean value. So we could write:
boolean areTheyEqual = ( x == y );
So aretheyEqual would be "true" if x was equal to y. Now suppose you wanted a variable "areTheyDifferent". We could get that in two ways, either using the "not" operator, which works on a boolean value:
boolean areTheyDifferent = ! areTheyEqual;
or we could use the "notEqual" comparison
boolean areTheyDifferent = ( x != y );
So the ! operator takes a boolean value and "inverts" it. You need to read the
!=
as a single comparison operator, just like >= is a single operator.
Comparison operators are used in logical statements to determine equality or difference between variables or values.
eg x!=y
Logical operators are used to determine the logic between variables or values.
eg !(x==y)
Related
I have written some code and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?
Example:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I’m changing and in the other I’m checking. But in both I am referring to equality.
= is the assignment operator. It sets a variable (the left-hand side) to a value (the right-hand side). The result is the value on the right-hand side.
== is the comparison operator. It will only return true if both values are equivalent after coercing their types to the same type.
=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
= assigns a value to a variable
== checks if the two parameter are equal to each other
=== checks if the two parameters are equal to each other and if their type is the same
! not operator
!= checks if the two parameters are not equal to each other
!== checks if the two parameters are not equal to each other or the type is not the same
one more
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
>== DOESN'T EXIST
etcetera...
== is used to test if the value on the left is equal to the value on the right.
= is used to assign the value on the right to the variable on the left.
In javascript you have also the ===.
= This is for set the value to the variable.
== This is for compare if the value is the same.
=== This is for compare if the value is the same and also the type is the same.
The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.
Assignment Operators
Comparison Operators
I have written some code and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?
Example:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I’m changing and in the other I’m checking. But in both I am referring to equality.
= is the assignment operator. It sets a variable (the left-hand side) to a value (the right-hand side). The result is the value on the right-hand side.
== is the comparison operator. It will only return true if both values are equivalent after coercing their types to the same type.
=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
= assigns a value to a variable
== checks if the two parameter are equal to each other
=== checks if the two parameters are equal to each other and if their type is the same
! not operator
!= checks if the two parameters are not equal to each other
!== checks if the two parameters are not equal to each other or the type is not the same
one more
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
>== DOESN'T EXIST
etcetera...
== is used to test if the value on the left is equal to the value on the right.
= is used to assign the value on the right to the variable on the left.
In javascript you have also the ===.
= This is for set the value to the variable.
== This is for compare if the value is the same.
=== This is for compare if the value is the same and also the type is the same.
The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.
Assignment Operators
Comparison Operators
I have written some code and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?
Example:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I’m changing and in the other I’m checking. But in both I am referring to equality.
= is the assignment operator. It sets a variable (the left-hand side) to a value (the right-hand side). The result is the value on the right-hand side.
== is the comparison operator. It will only return true if both values are equivalent after coercing their types to the same type.
=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
= assigns a value to a variable
== checks if the two parameter are equal to each other
=== checks if the two parameters are equal to each other and if their type is the same
! not operator
!= checks if the two parameters are not equal to each other
!== checks if the two parameters are not equal to each other or the type is not the same
one more
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
>== DOESN'T EXIST
etcetera...
== is used to test if the value on the left is equal to the value on the right.
= is used to assign the value on the right to the variable on the left.
In javascript you have also the ===.
= This is for set the value to the variable.
== This is for compare if the value is the same.
=== This is for compare if the value is the same and also the type is the same.
The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.
Assignment Operators
Comparison Operators
I have written some code and in certain places == is required and in others = is required. Can someone explain the differences or point me in the direction of the resource that can?
Example:
if($("#block").css.display == "none"){
$("#block").css.display = "block";
}
The only thing I can come up with is that in one I’m changing and in the other I’m checking. But in both I am referring to equality.
= is the assignment operator. It sets a variable (the left-hand side) to a value (the right-hand side). The result is the value on the right-hand side.
== is the comparison operator. It will only return true if both values are equivalent after coercing their types to the same type.
=== is a more strict comparison operator often called the identity operator. It will only return true if both the type and value of the operands are the same.
I would check out CodeCademy for a quick intro to JavaScript.
If you prefer to read more, MDN is a great intro as well.
For those concerned about the source of the term "identity operator" jbabey pointed out that JavaScript: The Definitive Guide seems to mention it.
= assigns a value to a variable
== checks if the two parameter are equal to each other
=== checks if the two parameters are equal to each other and if their type is the same
! not operator
!= checks if the two parameters are not equal to each other
!== checks if the two parameters are not equal to each other or the type is not the same
one more
> checks if one parameter is greater than the other
>= checks if one parameter is greater than or equal to the other
>== DOESN'T EXIST
etcetera...
== is used to test if the value on the left is equal to the value on the right.
= is used to assign the value on the right to the variable on the left.
In javascript you have also the ===.
= This is for set the value to the variable.
== This is for compare if the value is the same.
=== This is for compare if the value is the same and also the type is the same.
The = operator is an assignment operator. You are assigning an object to a value. The == operator is a conditional equality operation. You are confirming whether two things have equal values. There is also a === operator. This compares not only value, but also type.
Assignment Operators
Comparison Operators
This question already exists:
What does `!!~` mean in javascript? [duplicate]
Closed 8 years ago.
I was reading this blog post which mentioned using:
!!~
I have no idea what this does? at first I thought it would give an error, but the code below does run:
var _sessions = [
"_SID_1",
"_SID_2",
"_SID_3",
"_SID_4"
];
if(!!~_sessions.indexOf("_SID_5")) {
console.log('found');
} else {
console.log('!found');
}
output:
node test.js
!found
~ is the bitwise not operator. It inverts the bits of its operand. ! is the logical not operator. The bitwise not operator will return 0 when applied to -1, which is what indexOf returns when the value is not found in the array. Since 0 evaluates to false, doubly negating it will simply return false (a boolean value, rather than a numeric one):
var index = _sessions.indexOf("_SID_5");
console.log(~index); // 0
console.log(!~index); // true
console.log(!!~index); //false
The bitwise not operator will return a value less than 0 for any other possible value returned by indexOf. Since any other value will evaluate to true, it's just a shorthand method (kind of... they are both the same number of characters!) of checking whether an element exists in an array, rather than explicitly comparing with -1:
if (_sessions.indexOf("_SID_5") > -1) {
// This would work the same way
}
Update
With regards to the performance of this, it appears (in Chrome at least) to be marginally slower than the more common comparison with -1 (which itself is marginally slower than a comparison with 0).
Here's a test case and here's the results:
Update 2
In fact, the code in your question can be shortened, which may have been what the author was attempting to do. You can simply remove the !!, since the ~ will always result in 0 or below (and 0 is the only value that will evaluate to false):
if (~_sessions.indexOf("_SID_5")) {
// This works too
}
However, in a slightly different situation it could make sense to add in the ! operators. If you were to store the result of the bitwise operator in a variable, it would be a numeric value. By applying the logical not operator, you get a boolean value (and applying it again ensures you get the correct boolean value). If for some reason you require a boolean value over a numeric one, it makes a little bit more sense (but you can still just use the normal comparison with -1 or 0):
var inArray = !!~_sessions.indexOf("_SID_5");
console.log(typeof inArray); // boolean
Donald Knuth: "[...] premature optimization is the root of all evil"
For the sake of readability: please use
.indexOf !== -1
This explains it well:
The tilde operator in Javascript
Mixing the two NOT operators together can produce some interesting results:
!~(-2) = false
!~(-1) = true
!~(0) = false
!~(1) = false
!~(2) = false
So this just checks if the value equals -1 or not, and indexOf returns -1 if it does not find a match