If statement issue enter [duplicate] - javascript

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')){ }

Related

js variable comparison. Short version [duplicate]

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');
}

Why does 1 == 1 == 1, but not 'A' == 'A' == 'A' [duplicate]

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

In javascript, 5 || 0 and 0 || 5 returns 5 [duplicate]

This question already has answers here:
What does the || operator do?
(4 answers)
Proper use of ||
(3 answers)
Javascript-like || in php [closed]
(1 answer)
Closed 8 years ago.
As the title says everything. 5 || 0 and 0 || 5 returns 5 in JavaScript. Why does this happen and what does two || means in javascript?
It's a boolean or, and 5 evaluates to truthy. If you want to force your types to boolean you should use the !! (double negation) like so,
!!(5 || 0)
|| is a boolean or.
5 == true
0 == false
So, 5 || 0 = 5
The || is a synonym for the logical OR
So the statement ANY_VALUE || ANY_OTHER_VALUE means that if the first value is truthy then return that else return the second value

Should I use == or === In Javascript? [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Difference between == and === in JavaScript [duplicate]
(2 answers)
Closed 8 years ago.
I am learning Javascript with codecademy, and I was doing some comparisons, and for my code I did:
`console.log(1 == 2)`
and it returned False.
I also did:
`console.log(2*2 === 3)`
and that also returned False.
To check that I have not made a mistake, I did:
`console.log(1 == 1)`
and that returned True
The instructions tell me that === means equal to.
Are there any problems with using == instead of ===? And, which is better to use and why?
Thanks for any help you can give me!
Using == compares only the values, === compares the type of the variable also.
1 == 1 -> true
1 == "1" -> true
1 === 1 -> true
1 === "1" -> false, because 1 is an integer and "1" is a string.
You need === if you have to determine if a function returns 0 or false, as 0 == false is true but 0 === false is false.
It really depends on the situation. It's usually recommended to use === because in most cases that's the right choice.
== means Similar while
=== means Equal. Meaning it takes object type in consideration.
Example
'1' == 1 is true
1 == 1 is true
'1' === 1 is false
1 === 1 is true
When using == it doesn't matter if 1 is a Number or a String.
http://www.w3schools.com/js/js_comparisons.asp
== is equal to || x==8 equals false
=== is exactly equal to (value and type) || x==="5" false
meaning that 5==="5" false; and 5===5 true
After all, it depends on which type of comparison you want.

Javascript: How to check if a string is empty? [duplicate]

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
}

Categories