0

EDIT This question has been answered, but I've chosen to clarify it for future readers.

I've studied the explanations given about the logical operator !(parameter) and the inequality operator !==. They do not though clarify in what way they differ from each other. Let's look at an example.

!($.inArray(latlng, markers) == -1)

and

$.inArray(latlng, markers) !== -1

What is the difference between theese two expressions and are there any performance issues with either of the solutions making the other more preferable?

Community
  • 1
  • 1
Kristofer Gisslén
  • 1,032
  • 1
  • 9
  • 26

3 Answers3

2

In your case there is no difference [due to aforementioned De Morgan's law], so stick to what is more readable to you.


Worth adding:

Comparison operators are binary operators -> have a arity value of 2, because they compare two things.

On the other hand, the negation operator (!) has arity one, because it negates its only argument to its boolean counterpart -> its image is thus [true, false].

This is why you may encounter !!foo in your programmer's life as a way to coerce foo to a boolean.

moonwave99
  • 19,895
  • 2
  • 38
  • 62
1

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)

Mushahid Khan
  • 2,649
  • 1
  • 17
  • 30
1

There really isn't a whole lot of difference, just the order that the logic operates. The first statement checks if they are equal and then gives you the opposite result. The second checks to see if they are different by comparing the values to see if they are the same and returning false if they are.

In effect, they are the same. There is actually something called DeMorgan's Law that governs the equality between comparison and logical operators (i.e. stating that they are equivalent and can be exchanged for others that have the same result in a different structure)

Ashish Kafle
  • 147
  • 8
Patrick548
  • 310
  • 3
  • 16
  • This answer really unlocked some mystery to me. And thanks for the informative link. How about performance? Will any of them effect performance negatively? – Kristofer Gisslén Apr 24 '15 at 14:02
  • 1
    Performance really depends on how the code is compiled, but most compiles understand DeMorgan's Law and will find the optimal way to write the code into memory. Overall, though, any performance impacts would probably be negligible unless you are writing the code at the assembly level, in which case you could calculate the number of cycles needed for each case and determine which is faster. Most of the application of this is in Karnaugh Maps which are used for removing states that don't matter form complex equations. http://en.wikipedia.org/wiki/Karnaugh_map – Patrick548 Apr 24 '15 at 14:07