JavaScript: Declaring VAR with 'or' statement - javascript

the code is something like this
var $page_products = $page_name == 'products-uninterruptible-power-supply.php' OR 'products-uninterruptible-power-supply-np2031.php';
i already tried using
var $page_products = $page_name == 'products-uninterruptible-power-supply.php' || 'products-uninterruptible-power-supply-np2031.php';
but it is still not working. thanks in advance!

Assuming your intention is to declare one variable, $page_products, which will be set to true if the $page_name is equal to either of those two strings and false otherwise, do this:
var $page_products = $page_name == 'products-uninterruptible-power-supply.php'
|| $page_name == 'products-uninterruptible-power-supply-np2031.php';
EDIT: I just noticed that both page names start with the same four words, so instead of comparing to each string it may suit you to test whether the value in $page_name starts with those words, e.g.,:
var $page_products =
$page_name.indexOf('products-uninterruptible-power-supply') === 0;

This should work
var $page_products = ($page_name == 'products-uninterruptible-power-supply.php' || $page_name == 'products-uninterruptible-power-supply-np2031.php');

If you want to assign the file name conditionally, then you'll want the ternary operator:
var $page_products = ($page_name == 'products-uninterruptible-power-supply.php') ?
'products-uninterruptible-power-supply.php' :
'products-uninterruptible-power-supply-np2031.php';

Related

How can I rewrite this conditional statement to be shorter?

I have a conditional statement with an && and || clause but I would like to shorten and clean this up. Would the best course of action be to rewrite this inside of an array? Should I also turn this into a ternary operator?
Thanks for your help
if ((V1_CCLS == "S06") && ((V1_CCCT === "") || (VS_SELQ == "LED_COUNT_CRNR"))) {
V1_CCCT = V1_CCLS;
}
It would be easier to read if you put it into a variable or function and name is something descriptive so whoever is reading it knows quickly what is going on.
Also, you want to use parentheses with the same rules as algebra. Putting parentheses around (V1_CCLS == "S06") isn't needed, for instance.
For example:
let isReady = V1_CCLS === "S06" && (V1_CCCT === "" || VS_SELQ === "LED_COUNT_CRNR");
if (isReady) {
V1_CCCT = V1_CCLS;
}
I wouldn't use a ternary for this case. I'd just make it a bit more readable by displaying the conditions on separate lines.
if (
V1_CCLS == "S06" &&
(
V1_CCCT === "" ||
VS_SELQ == "LED_COUNT_CRNR"
)
) V1_CCCT = V1_CCLS;
V1_CCCT = (V1_CCLS == "S06") && (!V1_CCCT) || (VS_SELQ == "LED_COUNT_CRNR")) ? V1_CCLS : ""

How to combine two value checks into one line in javascript

I have the following code in javascript:
var seasonDropdown = TVContainer.find('select.season').val()
if (seasonDropdown == "-1" || !seasonDropdown) {seasonDropdown = null}
Is there a way to combine these two into one line?
You could do the following:
var seasonDropdown = (TVContainer.find('select.season').val() == "-1" || !TVContainer.find('select.season').val()) ? null : TVContainer.find('select.season').val();
But honestly, you should prefer readability over a solution like this.
if you want a bit cleaner look instead, you could use this:
var seasonDropdown = TVContainer.find('select.season').val();
if (seasonDropdown == "-1" || !seasonDropdown) seasonDropdown = null;

Passing condition to a var in javascript

I don't know how to solve this. I need something like this:
var condition;
if (a==1) //a comes from another part
condition = "arr3[cliente].año == year";
if (a==2)
condition = "arr3[cliente].año == year && arr3[cliente].sic"
//now another if
if (condition){
//rest of code
}
I need different conditions depending some previous values, the code inside the last if is always the same, so I don't need:
if (arr3[cliente].año == year)
// code
else if (arr3[cliente].año == year && arr3[cliente].sic)
// code
How can I do it?
Just assign the result of the expressions to the variable (currently you are assigning a string). The expression doesn't have to be inside the if statement, the result is what's important:
var condition;
if (a==1)
condition = arr3[cliente].año == year;
if (a==2)
condition = arr3[cliente].año == year && arr3[cliente].sic;
// It this point `condition` will either have the value `undefined`, `true` or `false`.
if (condition) {
// code
}
Of course you can simplify/reduce this to the following:
if (arr3[cliente].año == year && (a == 1 || a == 2 && arr3[cliente].sic)) {
// code
}
No need for repeating if statements or comparisons. This also assumes that accessing any of the properties doesn't have any side effects.
Just use the conditions you have, but not as strings. They'll either be true or false and your if(condition) check will still work fine.
var condition;
if (a == 1) //a comes from another part
condition = arr3[cliente].año == year;
if (a == 2)
condition = arr3[cliente].año == year && arr3[cliente].sic;
//now another if
if (condition) {
//rest of code
}
My proposition. if a==1 you will need the rest of condition and if a==2 you'll need second option condition.
if (a==1 && (arr3[cliente].año == year)){
commonFunction();
} else if (a==2 && (arr3[cliente].año == year && arr3[cliente].sic)){
commonFunction();
}
function commonFunction() {
//rest of code
}

What is the "angular" way to perform a conditional ng-if check?

Say I have a string that contains a value of "HOMEPAGE"
vm.string = "HOMEPAGE"
and I want to perform an ng-if inside of a div that causes it to display if vm.string is equal to one of these five strings.
"HOMEPAGE"
"ABOUT"
"STORE"
"EXAMPLE"
"SOMETHING"
I could do this using or operators inside of the ng-if to achieve the desired effect
<div ng-if="WhateverController.string == 'HOMEPAGE'
|| WhateverController.string == 'ABOUT'
|| WhateverController.string == 'STORE'
|| WhateverController.string == 'EXAMPLE'
|| WhateverController.string == 'SOMETHING'">
This will display because the string is 'HOMEPAGE'
</div>
I could also do this by creating a function that returns a boolean in my controller.
vm.isTrue = function () {
return (vm.string == 'HOMEPAGE'
|| vm.string == 'ABOUT'
|| vm.string == 'STORE'
|| vm.string == 'EXAMPLE'
|| vm.string == 'SOMETHING');
};
<div ng-if="WhateverController.isTrue">
This will display because the string is 'HOMEPAGE'
</div>
My question to you all is which of these two methods is considered the more "angular" way? I'm partial to doing this inside of the javascript because it makes the HTML look cleaner, but I am curious as to whether there is one way preferred over the other and why.
If you are going to use this check in only one place of your code, i'd say it doesn't matter except if you want your HTML to look cleaner. It's just a matter of preference.
Using a function on the other hand is better if you are going to check that condition several times in your code.
I think second one is better. It's not even Angular way, but way of writing clean code.
You should also avoid writing very long logical conditions and instead split it to several variabled. For example:
vm.age > 5 && vm.age < 100 && vm.name.length > 5 && (vm.location == "Spain" || vm.location == 'England')
You should instead use:
vm.isTrue = function(){
var isOldEnough = vm.age > 5;
var isYoungEnough = vm.age < 100;
var nameLengthIsCorrect = vm.name.length > 5;
var inSpainOrInEngland = ["Spain","England"].includes(vm.location);
return isOldEnough && isYoungEnough && nameLengthIsCorrect && inSpainOrEngland;
}
This way your code is self-explanatory.

Using conditionals within variable declaration

I'm trying to detect the "Do not track" setting on browsers... the origionla working code is:
if(navigator.doNotTrack == "yes" || navigator.doNotTrack == "1" || navigator.msDoNotTrack == "1"){
alert("true");
}else{
alert("false");
}
I'm trying to re-write it slightly and I'm wondering how to use a conditional within a variable declaration? I've come up with a not-working snippet that I was wondering if someone could help me with?
var DNT = navigator.doNotTrack,
msDNT = navigator.msDoNotTrack,
DNTtrue = "yes" || "1";
if(DNT === DNTtrue || msDNT === DNTtrue){
alert("true");
}else{
alert("false");
}
You can't to it like this. The expression "yes" || "1" is evaluated at the moment it is encountered. So you end up comparing DNT and msDNT to "yes". There is no way to tell JS to evaluate an expression later instead. In even then, DNT === "yes" || "1" or DNT === ("yes" || "1") would not yield desired results either.
Here is an alternative solution, which simply tests whether the value of DNT or msDNT exists are property in an object:
var DNTtrue = {"yes": true, "1": true};
if (DNTtrue[DNT] || DNTtrue[msDNT]) {
// ...
}
If DNT or msDNT have a different value than "yes" and "1", then DNTtrue[...] tries to access a non-existing property which will result in undefined (a falsy value).
In modern browsers you could use an array and the indexOf() method:
DNTtrue = ["yes", "1"];
if(DNTtrue.indexOf(DNT) > -1 || DNTtrue.indexOf(msDNT) > -1){
alert("true");
}else{
alert("false");
}
Try DNT.value inside if statement
Make DNTtrue=true not to yes

Categories