This question already has answers here:
How do you test for NaN in JavaScript?
(3 answers)
Can (a== 1 && a ==2 && a==3) ever evaluate to true?
(29 answers)
Closed 3 months ago.
I was reading the source code of core-js and I saw the following:
if (value != value) return true;
What does it actually mean? When exactly value won't be equal to itself?
It could be if:
value is NaN
console.log(NaN != NaN);
value is actually a getter on the global object, and it returns something different each time:
let i = 0;
Object.defineProperty(window, 'value', { get: () => i++ });
console.log(value != value);
Related
This question already has answers here:
How do I test if a variable does not equal either of two values?
(8 answers)
What's the prettiest way to compare one value against multiple values? [duplicate]
(8 answers)
Closed 2 years ago.
I am experimenting with JavaScript conditions. In the code below, why is my first condition not returning true while the second one does?
Javascript:
arr = [1,2,3];
if (arr[0] !== (1 || 2)){console.log('true')}
//undefined
if (arr[0] !== (2 || 1)){console.log('true')}
//true
|| will evaluate to either:
The first value, if it's truthy, or
The final value (which may be either truthy or falsey)
Since 1 is truthy, (1 || 2) evaluates to 1:
if (arr[0] !== (1 || 2)){console.log('true')}
// resolves to
if (1 !== (1 || 2)){console.log('true')} // resolves to
if (1 !== (1)){console.log('true')} // resolves to
if (1 !== 1){console.log('true')} // resolves to
if (false){console.log('true')}
So the first if statement does not go through. In the second case, 2 || 1 evaluates to the first truthy value of 2, so the if statement succeeds.
For this, you need an array of numbers to check against and negate the check as well.
const arr = [1, 2, 3];
if (![1, 2].includes(arr[0])) {
console.log(arr[0], 'is not', 1, 'or', 2);
}
if (![1, 2].includes(arr[2])) {
console.log(arr[2], 'is not', 1, 'or', 2);
}
This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 4 years ago.
I brow some code in javascript, found some code of function like that
function isPromise(obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
}
As what I say to the title, why this function use !!obj" ??
If you are referring to "double bang" in JavaScript it is a force coercion falsely or truthy to its boolean true or false
See this answer:
What is the !! (not not) operator in JavaScript?
This question already has answers here:
What is "x && foo()"?
(5 answers)
What does this symbol mean in JavaScript?
(1 answer)
What a strange syntax?
(3 answers)
Closed 4 years ago.
jBox.prototype.position = function (options)
{
// this line
!options && (options = {});
}
In conventional programming boolean statements are used in if else statements.
What does !options && (options = {}); translate too?
options is a json or array. What does !options mean?
options = {} is assigning a empty json to variable options, how does it return a boolean value to be used with &&.
The code:
!options && (options = {});
is the equivalent of:
if(!options) {
options = {};
}
It means that if options is a falsy value (empty) then initialize it with an empty object literal.
This question already has answers here:
Can (a== 1 && a ==2 && a==3) ever evaluate to true?
(29 answers)
Closed 4 years ago.
I want to run this if statement .
Help me to get the value of a.
var a =undefined ;
if(a=1 && a=2 && a=3)
{
console.log("Hello")}
else
{console.log("Error")
}
The issue is with this line if(a=1 && a=2 && a=3).Here the value is getting set to a instead of checking the condition
var a = undefined;
if (a === 1 && a === 2 && a === 3) {
console.log("Hello")
} else {
console.log("Error")
}
i want to run this if statement
a cannot be 1,2,3 at same time unless you are looking for something like this.Otherwise you can use or operator || instead of &&
This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 6 years ago.
var how = $('how');
var id = $('myId');
if ((how != null && !how.value.blank()) || myId != null) {
return true;
} else {
alert('Error');
}
Is there an easier way to check for not null and checking if the value of that element is blank without having to do both != null and then calling value?
Since null is falsy, a slightly shorter version would be
if((how && !how.value.blank()) || myId != null) {
...
}
Note that the above code and your own snippet both assume that if how exists, it will have a property called value, and will throw an exception if this is not the case.