Javascript, better way to write an if condition [duplicate] - javascript

This question already has answers here:
Check variable equality against a list of values
(16 answers)
Closed 3 years ago.
Is there a concise or better way to write this condition without having to use so many ||.
if (myVar != 'A' && myVar != 'B' && myVar != 'C'){
...
}

You can also express it like an array,
if (!['A', 'B', 'C'].includes(myVar)) {
// if myVar is none of the values in the array
}
edit: added negation to grab the opposite case as your example

Related

how to check that a variable contains something in JavaScript [duplicate]

This question already has answers here:
if else statement reverse
(4 answers)
Closed 12 days ago.
Good morning
there are many tutorials that explain how to check if a string is empty but not the other way around
so i would like to do this
if variable not empty then we do this
if not empty then we do this
//do the opposite
if (nomvalidation === null || nomvalidation.trim() === ""){
}
Simply change the equality operators to inequality and the or to and and.
if (nomvalidation !== null && nomvalidation.trim() !== ""){
}
You may also want to look into the typeof operator to make sure its a string and undefined as another possible "empty" state.

Shorthand for multiple or operator to check if different variables are an empty string [duplicate]

This question already has answers here:
Check variable equality against a list of values
(16 answers)
Closed 1 year ago.
Is there a shorthand for the following?
startsWith !== '' || contains !== '' || endsWith !== ''
I am looking for something more clean but not the overkill
Assuming that they are variables, you could create an array from startsWith, contains and endsWith and then check the array contains an empty string like below:
const array = [startsWith, contains, endsWith];
if (!array.includes('')) {
...
}

How to check if a "deep" structure is not undefined? [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 2 years ago.
what is the best aproach to check if a "deep" structure is undefined?
For example I have some check like this:
if (json.error.message != undefined) {
console.log(json.error.message);
}
The problem here is, that if json.error is already undefined this will crash. So I would need to write something like:
if (json.error != undefined && json.error.message != undefined) {
console.log(json.error.message);
}
But this could not be the best aproach or? When I would have even "deeper" structures.
if (json?.error?.message != undefined) {
console.log(json.error.message);
}

Is there a conditional assignment shorthand in javascript? [duplicate]

This question already has answers here:
JavaScript shorthand ternary operator
(10 answers)
Closed 5 years ago.
Can this be done in javascript
python code:
value = value if value else "Oompa Loompa"
Update:
the duplicate solution proposed does not answer my question, i would like something similar to what python does in js if possible, i have supplied another example for python below:
value = value if value >= 5 else 0
you can do something like
value = value || "Oompa Loompa"
or
value = value ? value: "Oompa Loompa"
You're looking for the ternary operator:
value = value ? value : "Oompa Loompa"

if(a = 1) is true [duplicate]

This question already has answers here:
Variable assignment inside an 'if' condition in JavaScript
(6 answers)
Closed 6 years ago.
I don't know if it's possible duplicate. Testing my code, sometimes I was wrong with because I put = and not == or === in if conditions:
Consider this code :
var a = 0;
if(a = 1) console.log('true');
I don't know why this is not an error and why returns true the condition (a = 1)
I guess that what it does is assign 1 to the a variable, but why this evaluates to true and there's no error?
you're setting a to 1 and then checking the truthiness of the result. Non-zero numbers in JavaScript are true, so you get what you see.
Like in math, things are evaluated left-to-right, with parens going first.
As it was said, it does assign to your variable and will return true for all values other than 0.
A way to avoid these kind of mistakes is to change the test.
if( 3 == a)
Here if you ever write (3 = a) you would have an error.

Categories

Resources