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

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.

Related

JavaScript checking for nullish value as part of an if block? [duplicate]

This question already has answers here:
How can I determine if a variable is 'undefined' or 'null'?
(34 answers)
Closed last month.
Is it possible to check for nullish values as part of an if block, without resorting to a pair of explicit checks for null and undefined.
For example, don't want to do this:
if(key === null || key === undefined) {
// do something
}
This doesn't work, because it also includes other falsy values such as zero and empty array.
if(!key)
That's one of the good use cases of the operator ==:
if(key == null) {
// do something
}
This will work with both null and undefined, while keeping away other falsy values.

How to simplify simple statement in typescript or js [duplicate]

This question already has answers here:
Understanding JavaScript Truthy and Falsy
(9 answers)
Closed 5 months ago.
I would like to simplify this expression but don't know how. There has to be a way to have the same expression without the three conditions.
if ( request.Document != null &&
request.Document != undefined &&
request.Document != "" )
Just for context I am talking something similar to c#'s !string.isNullOrEmpty()
if (request.Document) {
...
}
as null, undefined, '' will all return false.
(but [], {} or ' ' will return true)
Check the equality table for more info about other values.

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

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

Call a function when two conditions are right using IF statement [duplicate]

This question already has answers here:
Logical Operators (AND Operators)
(3 answers)
What does this symbol mean in JavaScript?
(1 answer)
Closed 4 years ago.
I need to know how to look for two values in if statement.
I know about a method where the code will be executed when at least one value matches the specified number like in here:
if (x == 0 || y == 0) {
// code to be called
}
But the code above will be executed even if one value matches 0 and the other not. What's the way to check if both x and y are equal to 0?
I couldn't find the answer anywhere...
Use the AND operator instead of the OR operator, that is
if (x == 0 && y == 0) {
// code to be called
}
You want to use AND, which is &&

What is the difference between != null and !== null? [duplicate]

This question already has answers here:
Difference between == and === in JavaScript [duplicate]
(2 answers)
Closed 6 years ago.
Sorry for what I'm sure had have been asked in the past, but it's very hard to search for a question like this. "!=" and "!==" are not exactly search friendly. If anyone knows a duplicate question you can point me to it.
What is the difference between doing myVar != null and myVar !== null?
I know that != is not equal and !== is not equal value or not equal type, but when comparing to null is there ever a case where they would return different results? Is one better to use than the other?
The answer to the specific question about whether there's ever a case where != and !== comparisons involving null get different answers is yes:
undefined != null // false
undefined == null // true
undefined === null // false
undefined !== null // true
The rules for == and != explicitly include a clause that stipulates that null and undefined are the same.
Personally — that is, in my code — that fact is a reason for using != (or ==) when checking for null in cases where undefined should be treated the same way (which is a pretty common situation).

Categories

Resources