categoryId = categoryId === '' && location.pathname.match(regExp) ?location.pathname.match(regExp)[1] : categoryId
I know this is a ternary operator but what does categoryId === '' && location.pathname.match(regExp) do here? particularly no clue on '' && location.pathname.match(regExp)
is it a boolean?
So I will take you through the piece of code you have provided as detailed as my free time now can permit me and from the top of my head.
categoryId is a variable that is accepting the result of what is on the right. thus the = sign.
categoryId === '' This part here is simply making a comparison between the results in categoryId to an empty string '' which will return a boolean. So its saying "is the result in categoryId an empty string? true or false.
&& this is saying that we are going to check under condition so check the above one and another...
location.pathname.match(regExp) this is the 2nd check... checking if a url path matches a certain regex definition/condition in regExp (you have not provided that so I can't say much there)
Now note that due to the use of &&, both conditions must return true before the true statement is run else it will be false.
? this is saying; if true, run the next condition/statement
location.pathname.match(regExp)[1] this is the condition/statement it will run if result is true.
: this means if it is false, run the next condition/statement
categoryId this is the condition to run when the result is false.
This type of conditional statement is called a "Conditional/Ternary Operator" find a bit more details here: https://www.w3schools.com/js/js_comparisons.asp
I hope this helps. If its not clear let me know so I clarify. Best way to learn!
Related
I want to push an error message to a field if it is invalid.
this code works
let message = [];
if (!isvalid) {
message.push("Please enter a value");
}
How can I achieve this with ternary operator.
The way I did.
message.push((!isvalid) ? "Please enter a value" : null)
But this code is also pushing null to the array.
like:: message=[null]
Honestly, it's not a good way to do it. It does not make sense to use a ternary where one logical branch of the condition leads to ...do nothing. Ternary only makes sense where you want to return one of two values depending on the condition.
If you want to be concise you could use:
!isValid && message.push('foo');
though some linters won't like you for it. It has the disadvantage that it is less readable than a simple if. If you must use the ternary, you could also do this:
!isValid ? message.push('Please enter a value') : void 0; // or null
but it's ugly and bad because of that useless hanging false branch of the ternary expression. Don't do it.
Can you try (!isvalid) ? message.push("Please enter a value") : null ?
I found the following in a code example at datatables.net.
return value > 20 ? true : false;
Why wouldn't they have just written
return value > 20;
Could the latter return values other than true or false? Trying to figure out whether they were maybe just thinking the code was more readable this way, or whether there is actually a significant reason for doing this that I'm not aware of.
The only possible result is true or false. I don't think it makes it more readable. The only reason they may have done it that I can think of is that they were a new developer and didn't realize value > 20 was valid to return.
This is along the same lines as someone writing this:
if(value > 20 === true){ . . . }
Which is unnecessary because if conditions are implicitly compared against their "truthy-ness". The statement should be:
if(value > 20){ . . . }
As such, in your example, the code should just be:
return value > 20;
Because (as you correctly surmize) a greater-than/less-than expression can only result in true or false.
Now, if someone wanted to return an alternate set of binary results, then you could see something like this being used:
return value > 20 ? "acceptable" : "unacceptable";
I'm trying to understand what exactly the double exclamation mark does. Yes, I saw this question, with lots of answers. So I know in principle what it does, but I don't know why one would ever need to use it.
From what I understand, it converts the value to a boolean. So let's say I have the following code:
var myBool = !!(index === 0 || index > len);
Can't I just leave out the !! and I will get the same result:
var myBool = (index === 0 || index > len);
What do I gain by adding !!? Is't it already a boolean vaule?
The purpose of !! is to canonicalize any type of truthy or falsey value to the corresponding boolean value.
If the value is already known to be a boolean, such as the result of a comparison operator, there's no point in it and it's redundant. So it's useless in the example you gave.
Ok, I've got an issue I don't understand.
I have a boolean value which I test and if true I do something.
BUT javascript never go in it even if the var is true.
I try this :
if(isConfigD)
handleConfigurationD;
this :
if(isConfigD == true)
handleConfigurationD;
And this :
if(isConfigD === true)
handleConfigurationD;
But nothing work whereas isConfigD is always set on true :(
What am I missing ?
You condition works well, but if you call a function, then you need parenthesis for the call.
handleConfigurationD();
// ^^
handleConfigurationD is just an identifier. That statement is going to have one of two outcomes:
A ReferenceError
"Yup, that's a bit of data"
Presumably you have stored a function in it and want to call the function.
handleConfigurationD();
I'm very new to JS - only a couple days in.
Trying to write a very basic prompt evaluated by an if statement.
When I run the code below, the user is prompted, but the statement is never evaluated by the if statement.
Any help? -- I realize the answer is probably simple and obvious, but as a SUPER beginner, what do I do?
var bool = prompt("What is an example of a boolean?");
if (typeof(bool) === "boolean") {
print("correct! that is a boolean");
print(bool) ;
};
In this case, assuming the user inputs something in the prompt, the type of the bool variable will always be a string. You'd rather check if the input compares to the string "true" or "false" etc., like this:
if (bool.toLowerCase() == "true" || bool.toLowerCase() == "false") {
...
}