This question already has answers here:
Check variable equality against a list of values
(16 answers)
Closed 22 days ago.
i need to compare a variable with multiple values
let test = 'e'
if(test == 'q' || test == 'w' || test == 'e') console.log('TEST')
Can you make this entry shorter?
I present it this way, but it is not correct.
let test = 'e'
if(test == 'q' || 'w' || 'e') console.log('TEST')
You can use an array:
const test = 'e'
if (['q', 'w', 'e'].includes(test)) {
console.log('TEST');
}
Related
This question already has answers here:
Javascript if (x==y==z): [duplicate]
(2 answers)
Closed 2 years ago.
I'm trying to write a flexible query logic based on a config payload, e.g:
{
"test": "( fieldA == fieldB == fieldC )"
}
When my app replaces 'fieldA', 'fieldB' and 'fieldC' value with text data, the result is always false.
This clearly has to do with me confusing the way javascript runs the comparison (it must have something to do with data types and math ordering), but can anyone explain this with a simple example for alphanumeric values?
e.g. 'John Smith' == 'John Smith' == 'John Smith' is always false;
x == y == z does not do what you think :
it translates to (x == y) == z.
With the quirks of javascript's == operator :
1 == 1 and 'A' == 'A' both translate to true
however : true == 1 returns true, while true == 'A' doesn't
You probably want to rewrite your condition to :
(x == y) && (x == z)
and very probably want to use the unambiguous === operator :
(x === y) && (x === z)
to avoid pitfalls such as the one you falled upon (true == 1 checks, but not true === 1)
A==B==C or A==A==A is legally allowed.
But the problem is, A==C never happens or in your case fieldA==fieldC never happens. Hence this leads to ambiguity
This question already has answers here:
Why is my c != 'o' || c != 'x' condition always true? [duplicate]
(5 answers)
Closed 5 years ago.
I have this JS var, followed by this if statement :
if ($('custom_t4'))
var contratType = $('custom_t4').value === 'Nouvelle_affaire' ? 'Nouvelle Affaire' : $('custom_t4').value === 'Avenant' ? 'Avenant' : '';
if (contratType && (contratType !== 'Nouvelle Affaire' || contratType !== 'Avenant')){ }
The problem I have, is that when contratType is defined and his value is 'Nouvelle Affaire', is still enter that if.
Did I miss something ?
In Second condition OR (||) Operator is the problem. Correct Code written below
if (contratType && (contratType !== 'Nouvelle Affaire' && contratType !== 'Avenant')){ }
This question already has answers here:
Why doesn't my simple if-statement render false in javascript?
(2 answers)
Closed 6 years ago.
I'm trying to check if a string is blank, less than or equal to 9 digits, or up to 10 digits. But it always follows the else if (str.length <= 9).
if (str = ''){
console.log("The string cannot be blank");
} else if (str.length <= 9) {
console.log("The string must be at least 9 characters long");
} else if (str.length <= 10) {
console.log("The string is long enough.");
}
No matter what I put in, I always get The string must be at least 9 characters long. Why?
= is always assignment. Equality comparison is == (loose, coerces types to try to make a match) or === (no type coercion).
So you want
if (str === ''){
// -----^^^
not
// NOT THIS
if (str = ''){
// -----^
What happens when you do if (str = '') is that the assignment str = '' is done, and then the resulting value ('') is tested, effectively like this (if we ignore a couple of details):
str = '';
if (str) {
Since '' is a falsy value in JavaScript, that check will be false and it goes to the else if (str.length <= 9) step. Since at that point, str.length is 0, that's the path the code takes.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Looking into the answer of Chris Brandsma in Advanced JavaScript Interview Questions what is === in Javascript.
If possible please provide a simple example
=== is the strict equal operator. It only returns a Boolean True if both the operands are equal and of the same type. If a is 2, and b is 4,
a === 2 (True)
b === 4 (True)
a === '2' (False)
vs True for all of the following,
a == 2
a == "2"
2 == '2'
=== is 'strict equal operator'. It returns true if both the operands are equal AND are of same type.
a = 2
b = '2'
a == b //returns True
a === b //returns False
Take a look at this tutorial.
please refer Strict Equality Check..
This question already has answers here:
How do I check for an empty/undefined/null string in JavaScript?
(52 answers)
Closed 3 years ago.
I know this is really basic, but I am new to javascript and can't find an answer anywhere.
How can I check if a string is empty?
I check length.
if (str.length == 0) {
}
If you want to know if it's an empty string use === instead of ==.
if(variable === "") {
}
This is because === will only return true if the values on both sides are of the same type, in this case a string.
for example:
(false == "") will return true, and (false === "") will return false.
This should work:
if (variable === "") {
}
But for a better check:
if(str === null || str === '')
{
//enter code here
}
if (value == "") {
// it is empty
}