This question already has answers here:
Javascript shorthand if
(5 answers)
Closed 9 years ago.
I would like to check whether variable a is equal to b or c.
Of course I know the explicit way to do this;
a === b || a === c
but is there a shorthand way of doing this in Javascript? I mean, for example,
a === (b || c)
does not work.
I found similar questions here
Short hand to do something like: if($variable == 1 || $variable == "whatever" || $variable == '492') .
PHP: If a equals b or c or d
but they are talking about PHP.
I don't think there's a shorter or more concise way of doing this in regular JavaScript.
An alternative would be to do a ternary, but this is arguably far less readable, so I would stick with the expression you have.
Ternary:
a === b ? true : a === c
Advised, as in your question
a === b || a === c
The simplest way is as you described a===b||a===c all other method can only expand your code. So, Use a===b||a===c instead of using any other .
((a === (b||c))
its the shortest way
Related
This question already has answers here:
Check variable equality against a list of values
(16 answers)
How to shorten my conditional statements
(15 answers)
Closed 3 years ago.
For example, instead of:
if (ext === "_st" || ext ==="_mt" || ext === "_00" ){
//do something
}
I would intuitively expect that the following also works:
if (ext === ("_st" || "_mt" || "_00" )){
//do something
}
But that doesn't work.
Is there a way to avoid repeating the same ("ext" in this case) variable for more compressed and efficient code?
UPDATE: I did a search before asking and none of the "duplicate" questions appeared in the first 10-15 suggestions. Instead, StackOverflow made WRONG suggestions for irrelevant -false duplicates.
Here is my suggestion:
Instead of marking questions as "duplicate" why not merge them along with the answers in one single question with an intuitive title?
Thanks for the answers.
UPDATE #2: This is NOT a duplicate. The suggested questions have answers that only refer to OR optimization while my question is more general, it refers to "logic expressions", NOT specifically to OR. I just provided an OR example. I'm specifically asking whether there is a method to avoid repeating the main variable in a logical statement whether it is OR, AND, etc.
For example: given var1 == var2 == var3
if (ext === var1 && ext === var2 && ext === var3 ){
//do something
}
var ext = "_00";
if (/^_st$|^_mt$|^_00$/.test(ext) ){
console.log("do something");
//do something
}
Using regex this is possible.
Explanation:
Each comparison is separated by an |(or) and enclosed in ^ and $ to match exactly otherwise it would match else where.
let ext = "_mt";
if( ext.match( /_st|_mt|_00/g ) ) {
console.log("Yes : type 1");
}
if( ["_st", "_mt", "_00"].includes( ext ) ) {
console.log("Yes : type 2");
}
As #Shily said you can use an array and this kind of regex too
This question already has answers here:
What does the construct x = x || y mean?
(12 answers)
Closed 8 years ago.
I have absolutely no idea how this is called which is the reason why my searches failed miserably.
What does this code mean
var a = b || c;
I believe it's something like a will equal which ever is defined but.. I just have to be sure what it exactly does before I use it.
If b is defined, it will use b, if c is defined and b isn't, it will use c. If you put it in parenthesis () it will return true/false.
This question already has answers here:
Using &&'s short-circuiting as an if statement?
(6 answers)
Javascript AND operator within assignment
(7 answers)
Closed 8 years ago.
I have following line of code in javascript:
q && (c = q === "0" ? "" : q.trim());
WHat does it mean? I understand that c is equals either empty string or q.trim() result, but what does mean q && ()?
JavaScript optimizes boolean expressions. When q is false, the right hand side of the expression doesn't matter for the result, so it's not executed at all. So this is a short form of:
if( q ) {
c = q === "0" ? "" : q.trim()
}
It is a guard against undefined/null/false variables.
If q is not defined, false or set to null, the code short circuits and you don't get errors complaining about null.trim()
Another way to write that would be:
if(q) {
c = q === "0" ? "" : q.trim();
}
You can use this kind of clause to check wether q is defined.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Hi,
This is my tiny code:
var domains_before_update = storage.getItem('domain_list_original');
if(domains_before_update==null || domains_before_update=="" )
{
gBrowser.selectedTab = gBrowser.addTab("chrome://filter/content/block_and_redirect_list.html");
}
Is that correct or should I be using === instead of == ?
Thanks!
=== checks the strict equals (without coercion) that you're used to , where == checks the value [after built-in coercion] equality
but as the other answer(s) noted, strict equality does not work when checking for null, use !variable
same as this post: Difference between == and === in JavaScript
edit: clarified some of the wording thanks to the helpful comments!
In this case, it doesn't matter - and in all cases where it doesn't matter, you should use strict equality or identity, e.g. ===.
Neither.
Use:
if(!domains_before_update)
{
}
For comparisons with null, === is required.
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?
Why do I see lots of javascript code lately with expressions that look like this:
if(val === "something")
Why "===" instead of just "=="? What's the difference? When should I use one or the other?
The === does not allow type coercion, so something like this would return false:
if (2 === '2') // false
The "normal" javascript == operator does allow type coercion, and so this would return true:
if (2 == '2') // true
var a = 3;
var b = "3";
if (a == b) {
// this is true.
}
if (a === b) {
// this is false.
}
=== is typically referred to as the identity operator. Values being compared must be of the same type and value to be considered equal. == is typically referred to as the equality operator and performs type coercion to check equality.
An example
1 == '1' // returns true even though one is a number, the other a string
1 === '1' // returns false. different datatypes
Doug Crockford touches briefly on this in JavaScript the Good Parts google tech talk video. Worth spending an hour to watch.
Checks that the type as well as the values match. This is important since (0 == false) is true but (0 === false) is not true.