Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am learning jQuery and I have seen that people in plugins use following quite a lot, and I don't know the meaning of each. So explanation of each would be really appreciated.
So here's the list, maybe I have type wrong some of but anyone is welcome to edit my post.
==, ===, !0, !1, !=, !==
Please explain to me... Thanks!
Check out this list of javascript logical operators:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Logical_Operators
and comparison operators:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Comparison_Operators
Operator Description
== is equal to
=== is exactly equal to (value and type)
!= is not equal
!== is not equal (neither value nor type)
> is greater than x>8
< is less than x<8
>= is greater than or equal to
<= is less than or equal to
!0 Not 0 (could be used as not false)
!1 Not 1 (could be used as not true)
source
// Comparison operators
var foo = 1;
var bar = 0;
var baz = "1";
var bim = 2;
foo == bar; // returns false
foo != bar; // returns true
foo == baz; // returns true; but note that the types are different
foo === baz; // returns false
foo !== baz; // returns true
foo === parseInt( baz ); // returns true
foo > bim; // returns false
bim > baz; // returns true
foo <= baz; // returns true
Link: http://stage.learn.jquery.com/javascript-101/operators/
Simply put, these are used to compare 2 values in an "if" statement. They are called comparison operators. Each one makes a different comparison. The most confusing are the 2= and the 3=. The third equals sign compares data types as well as value. More often than not, you will not need the 3rd = sign unless you are creating "strict" code. The operators break down like so:
== is equal to
Example: if (x == z) // this would return true if both x and z were the number 7
=== is exactly equal to (compares value and type)
Example: if (x == z) // if x is the String 7 and z is the Integer 7, this would return false
!= is not equal
Example: if (x != z) // if x is 7 and z is 8 then this if statement would return true
!== is not equal (again, compares both value and type)
Example: if (x !== z) // If both x and z are 7 but one is a String, while the other is an integer, then this would return true, because they are not the same type
> is greater than
Example: if (x > z) // If x is 8 and z is 7, then this would return true
< is less than
Example: if (x < z) // If x is 8 and z is 7, then this would return false
>= is greater than or equal to
Example: if (x >= z) // If x is 8 or 7 and z is 7, then this would still be true in either case
<= is less than or equal to
Example: if (x <= z) // If x is 8 and z is 7, then this would return false, but if x was 7 then it would be true
For more information, please follow these links:
http://www.w3schools.com/js/js_comparisons.asp
http://www.javascriptkit.com/jsref/comparison_operators.shtml
http://www.how-to-code.com/javascript/javascript-tutorial/comparison-operators-in-javascript.html
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 months ago.
Improve this question
I was playing around with on of the excercises when I coded this:
function isEven(x){
var m = x % 2
var n = x / 2
return m <= !n
}
The funny part is that it actually works, but I do not understand if !n makes the number negative or affects the boolean value itself. Can someone explain how my code works? Thanks.
!n is a red herring. For any value of x other than 0, !n will be false; it will be true when x == 0.
m is 0 for even numbers, 1 for odd numbers. When you compare a number with a boolean, the boolean is converted to a number. So m <= false is equivalent to m <= 0. This will be true when m is 0.
So you can get rid of n and just use this:
function isEven(x){
var m = x % 2;
return m <= false
}
[0, 1, 2, 3, 4, 5, 6, 7, 8].forEach(x => console.log(x, isEven(x)));
The reason why it works with !n is because in the only case where !n is true, x == 0 and m == 0. 0 <= true is true, so the function returns the correct result then as well.
First
var n = x / 2
n will be falsey if x is 0, and truthy otherwise.
If the argument is even and not 0:
m will be 0
n will be a truthy number (because anything divided by 2 is another non-zero number - except 0)
!n will be false (because that's the boolean inverse of 0, a falsey value)
the return statement is 0 <= false; false gets converted into a number when compared with <=, and 0 <= 0 is true
If the argument is odd and positive:
m will be 1
n will be truthy (because any odd number divided by 2 is another non-zero number, and a non-zero number is truthy)
!n will be false (because that's the boolean inverse of a truthy value)
the return statement is 1 <= false; false gets converted into a number when compared with <=, and 1 <= 0 is false
If the argument is even and 0:
m will be 0
n will be 0
!n will be true (boolean inverse of 0)
the return statement is 0 <= true; true gets converted into a number when compared with <=, and 0 <= 1 is true
But the code doesn't work when the argument is odd and negative, because a negative odd number % 2 gives -1, not 1.
m will be -1
n will be truthy (because any odd number divided by 2 is another non-zero number, and a non-zero number is truthy)
!n will be false (because that's the boolean inverse of a truthy value)
the return statement is -1 <= false; false gets converted into a number when compared with <=, and -1 <= 0 is true - when it should be false
Can someone please tell me what this snippet is doing?
Is it setting x to true if y is undefined?
var x = false;
var y = x === true;
The code
var x = false;
var y = x === true;
console.log(y);
is simply checking the condition x === true, like other programming language it will result to either true or false. Since you have var x = false; the condition x === true will result in false as false === true is always false. Now, the result of this condition is being assigned to the new variable y as var y = x === true; so the value for y will be false.
Set x to false
Is x exactly-equal to true?
2.1. If so, then set y to true
2.2. Otherwise, set y to false
There is no invokation of of global.undefined in this fragment.
The first line should be pretty clear, it sets x to false. The second line is a bit more difficult, first, it compares x to true. If x is exactly equal to true, it will set y to true. Since x is false, the comparison will also return false, so y will be set to false.
TL;DR, this is a convoluted way of setting y to false.
=== is a strict comparison operator. The value of variable y will only true if x is boolean and true, y will be false otherwise. Check this snippet out for example:
let a = 1;
console.log(a == true); // print true
let b = 1;
console.log(b === true); // print false, because `b` is a number, not boolean
let c = true;
console.log(c == true); // print true
let d = true;
console.log(d === true); // print true, because `d` is a boolean and its value is `true`
This var y = x === true; statement means: keep the returned value of (x === true) in the y variable. In your case, y will hold false as its value since x is a boolean, but its value is false.
Strict Equality Operator, === has a greater precedence over Assignment Operator, =. Therefore the check for x is done before assigning its value to y.
You can break this down into three statements.
var x = false
var tmp = (x === true)
var y = tmp
Since x is false, y will also get set to false.
Is there any value for what x === x returns false without NaN?
For example:
> x = 1
1
> x === x
true
> x = {}
{}
> x === x
true
> x = new Date()
Wed Nov 13 2013 15:44:22 GMT+0200 (EET)
> x === x
true
> x = NaN
NaN
> x === x
false
I see that the only value where x === x returns false is when isNaN(x) === true.
Is there another value of x for what x === x returns false? An official reference would be welcome!
The strict comparison between two equal non-NaN values will always be true (SLaks's answer correctly quotes the spec). However, it's possible for the expression x to change its value during the evaluation of the the equality. This can happen with property access when using accessor property descriptors (i.e., property getters):
foo = {};
Object.defineProperty(foo, "bar", {
get: function() {
return Math.random();
}
})
foo.bar === foo.bar; // false
If you do this for the global object window (or global in Node), then you can see the x === x comparison fail for a global-scope variable:
Object.defineProperty(window, "bar", {
get: function() {
return Math.random();
}
})
bar === bar; // false
The spec lists the exact rules for strict equality.
There are no other such cases, unless you count +0 and -0.
The SameValue algorithm (used for validating changes to read-only defined properties) has no such exceptions.
what value should X have so this condition would work?
// insert code here
if (x == 1 && x === 2) {
console.log('Succes!');
}
X should be defined like so:
Object.defineProperty(window,'x',{
get: function() {
this.__tmp = this.__tmp || 2;
this.__tmp = 3-this.__tmp;
return this.__tmp;
}
});
Then:
if( x == 1 && x === 2)
MIGHT work. Demonstration
The following code should do the trick (Demo here):
x = {};
x.valueOf = function (){
x = 2; // this is important
return 1;
};
if (x == 1 && x === 2) {
console.log('Success !!!');
}
Explanation:
The statements are executed from left to right (so first x == 1, then x === 2). When checking x == 1, it will go to the valueOf function, which returns 1, so it will be true. But at the same time, x is changed to be 2, thus the next statement that will be checked (x === 2) will also be true.
PS: As far as I know, this has no practical application. However, it can lead to better understanding of how javascript works - which is the point of such questions :)
X can't hold a value equals to 1 and identical to 2 at the same time, this expression is logically incorrect.
There is no such value.
x === 2 checks if x equals exactly to 2, while 2 cannot be 1 at the same time.
Only the following would make sense:
if (x && x === 2) { ... }
(or getter overloading, as demonstrated in #Niet the Dark Absol's answer, which is not a pure case)
Using === (identity) operator it will never work, but it's possible to construct an object that will be "equal" (==) to 1 and 2 at the same time:
x = { valueOf: function() { return this.foo ? 2 : this.foo = 1 } }
console.log(x == 1 && x == 2) // true
"valueOf" is the method JS implicitly calls when you convert or compare an object to a number.
Needless to say, this exercise doesn't have any practical sense.
I want to check the following
1: Is x a number
2. If x is less that 5 or greater than 15, sound alert
3. If all is ok, callMe()
var x = 10;
if (isNaN(x) && ((x < 5) || (x > 15))) {
alert('not allowed')
}
else
{
callMe();
}
What am I doing wrong?
var x = 10;
if (isNaN(x) || (x < 5) || (x > 15)) {
alert('not allowed')
}
else
{
callMe();
}
This way, if x is not a number you go directly to the alert. If it is a number, you go to the next check (is x < 5), and so on.
All the other answers about the && vs || are correct, I just wanted to add another thing:
The isNaN() function only checks whether the parameter is the constant NaN or not. It doesn't check whether the parameter is actually number or not. So:
isNaN(10) == false
isNaN('stackoverflow') == false
isNaN([1,2,3]) == false
isNaN({ 'prop' : 'value'}) == false
isNaN(NaN) == true
In other words, you cannot use it to check whether a given variable contains a number or not. To do that I'd suggest first running the variable through parseInt() or parseFloat() depending on what values you expect there. After that check for isNaN(), because these functions return only numbers or NaN. Also this will make sure that if you have a numeric string then it is also treated like a number.
var x = 10;
if (isNaN(x) || (x < 5) || (x > 15)) {
alert('not allowed')
}
else
{
callMe();
}