I have an boolean expression in javascript and i don't know what it means.
a = (b === LEFT && -2 || b === RIGHT && 2 || 0)
Please what does it mean ?
The && is a hacky shortcut if:
if (B === LEFT) {
a = -2;
} else if (B === RIGHT) {
a = 2;
} else {
a = 0;
}
one more shortcut with ternary operator
a = b === LEFT? -2: (b === RIGHT? 2 : 0)
Related
Is it possible to have conditions in return in JavaScript?
Like in this case:
if
(A > 0 || B > 0)
return "A";
else return "C"
As long as A or B is > 0 return the one that is > 0 (so A or B). Is that possible? Like another condition as return, for example?
Maybe this is similar to what you are trying to do. My function eval() takes two parameters A and B, and checks different conditions to see which is the positive number. I am using the Logical AND && instead of || because I want my code to know the value of each parameter in each condition.
function eval(A, B) {
if (A > 0 && B <= 0) {
return 'A';
}
if (A <= 0 && B > 0) {
return 'B';
}
if (A > 0 && B > 0) return "Both A and B are positive numbers";
}
console.log(eval(2, -5));
console.log(eval(0, 8));
console.log(eval(1, 1));
Try this.
return (A > 0 || B > 0) ? "A" : "C"
It's a ternary operator. Here you have more information:
Conditional (ternary) operator
if(A > 0 || B > 0)
return A > 0 ? "A" : "B";
else return "C";
Is there an efficient way to add two variables a and b when they could be dynamic? I also need to check if their types are numbers. I think the way I have described below is not the best/modern way to implement this. Please help.
EDIT
I also want to make sure if one the variable is not a number(or null) then the variable C gets its value from the variable which is the number . How can I do it?
const a;
const b;
let c;
// variables a and b are dynamic and could be null.
if(typeOf(a)==='number' && typeOf(b)==='number'){
if(!a && b){
c=b
}
if(a && !b){
c=a
}
if(a && b){
c=a+b
}
}
You can simply check with typeof and use the logical or operator to convert NaN to 0.
if(typeof a ==='number' && typeof b === 'number'){
c = (a || 0) + (b || 0);
}
To also allow invalid values to be treated as 0, you can use this:
c = (typeof a === 'number' ? (a || 0) : 0) + (typeof b === 'number' ? (b || 0) : 0)
It could also be written like so:
c = (typeof a === 'number' && !isNaN(a) ? a : 0) + (typeof b === 'number' && !isNaN(b) ? b : 0)
I think you're looking for
const c = (typeof a == 'number' ? a : 0)
+ (typeof b == 'number' ? b : 0);
I'm just starting to learn javascript and i'm trying to figure out how to make this code shorter.
Right now, the check function evaluates if a = "admin" and after that if a = "manager".
Is it possible to do this evaluation in one line?
Kind of "if (a = "admin" or "manager") ..."
const valid = "User name valid";
const invalid = "User name invalid";
function check(a, b) {
if (a === "admin") {
return valid;
} else if (a === "manager") {
return valid;
} else if (b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10) {
return valid;
} else {
return invalid;
}
}
console.log(check("manager", "ikey"));
console.log(check("admin", "root"));
console.log(check("user", "ikey"));
console.log(check("user", "Mikey"));
Thanks!!
You can use the logical or operator to handle all valid cases together.
function check(a, b) {
if (a === "admin" || a === "manager" || b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10) {
return valid;
} else {
return invalid;
}
}
You can also simplify this to one statement using the ternary operator.
function check(a, b) {
return a === "admin" || a === "manager" || b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10 ? valid : invalid;
}
You should do it with or operator || like so:
if (a === "admin" || a === "manager" || (b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10))
return valid;
else
return invalid;
I am trying to alert "yes" if ether of the conditions in my if statement are true:
var a = 2;
var b = 1;
if (a = 1 or b = 1 ) {
alert('yes');// should alert in this case
} else {
alert('no');
}
https://jsfiddle.net/90z7urvd/1/
What do I use for the if, if this is possible?
a = 1 will set the value 1 to variable a. It is not doing a comparison. For comparison, you use === or ==
=== (Identity operator) is the correct way to compare if both the types are same.
if (a === 1 || b === 1 ) {
=== operator won't do the type conversion before the comparison while == does the type conversion before the comparison.
For your or case, You may use || operator
var bootresul = someExpression || anotherExpression
Corrected code
var a = 2;
var b = 1;
if (a === 1 || b === 1 ) {
alert('yes');
} else {
alert('no');
}
You are assiging value rather then comparing
Try like this
if (a == 1 || b == 1)
To compare strictly use ===
Like this
if (a === 1 || b === 1)
JSFIDDLE
you can do this
var a = 2;
var b = 1;
if ((a == 1) || (b == 1 )) {
alert('yes');// should alert in this case
} else {
alert('no');
}
the == is one of the relational operator for checking equality and || is a logical operator that is a notion of logical OR
use this to compare just values
if (a == 1 || b == 1){
}
OR use this to compare values and type of variable
if (a === 1 || b === 1){
}
note : == will just check of values and === this will check value with type of variable
var a = 2;
var b = 1;
if (a == 1 || b == 1 ) {
alert('yes');// should alert in this case
} else {
alert('no');
}
I think you were doing assignment instead of comparison
Try using this:
if(a === 1 || b === 1){
alert('YES!')
}else{
alert('NO!')
}
OR you can use ternary operator condition instead of if else
(a == 1 || b == 1) ? alert('YES!') : alert('NO!')
How do you say if (A == 0) OR (B == 0)?
Just to be snarky:
if (A === 0 || B === 0)
if (A == 0 || B == 0)
or
if ((A == 0) || (B == 0))
Check out Control Structures and Operators on Wikibooks
if ( A == 0 || B == 0 ) {
}
depends if you mean exclusive or inclusive OR :)
Inclusive OR:
if(A == 0 || B == 0)
{
}
Exclusive OR:
if(A == 0 && B != 0 || A != 0 && B == 0)
{
}