How I can write the following without &&
?
if(a == 1 && b == 2) { ... }
Can I create a function for the operator?
How I can write the following without &&
?
if(a == 1 && b == 2) { ... }
Can I create a function for the operator?
Create a function to encapsulate your operation:
function compare(a, b, value1, value2) {
if(a === value1) {
if(b === value2) {
return true;
}
}
return false;
}
And you can use it like so:
if(compare(a, b, 1, 2)) {
// Your action..
}
You can do this like
if(a==1){
if(b==2){
JS function
}
}
Both will work the same but if(a==1 && b==2)
is a good approach to do exactly the same.
Not sure why you'd want to do this, but you could nest if
statements:
if(a == 1){
if(b == 2){
...
}
}
Or you could use a bitwise operator, if you really just need to consider 2
and 1
if(b >> a === 1){
...
}
There are a lot ways to do it, but it really depends on your data.
You could take advantage of prototypal inheritance, and create a constructor with its prototype extended with methods.
function Comparer(a, b) {
if (!(this instanceof Comparer))
return new Comparer(a, b);
this.assign(a, b);
this.compare();
}
Comparer.prototype.result = false;
Comparer.prototype.compare = function() {
this.result = this.a == this.b;
};
Comparer.prototype.assign = function(a, b) {
this.a = a;
this.b = b;
};
Comparer.prototype.and = function(a, b) {
this.assign(a, b);
if (this.result !== false)
this.compare();
return this;
};
Comparer.prototype.or = function(a, b) {
this.assign(a, b);
if (this.result !== true)
this.compare();
return this;
};
And use it like this:
var a = 1,
b = 2;
if (Comparer(a, 1).and(b, 2).result)
console.log("pass");
else
console.log("fail");
We could even extend it to get rid of the if
statement.
Comparer.prototype.then = function(fn) {
if (this.result === true)
fn();
return this;
};
Comparer.prototype.otherwise = function(fn) {
if (this.result === false)
fn();
return this;
};
And use it like this:
var a = 1,
b = 2;
Comparer(a, 1)
.and(b, 2)
.then(function() { console.log("pass"); })
.otherwise(function() { console.log("fail"); });
Or shorten things up like this:
var log = Function.bind.bind(console.log, console);
var a = 1,
b = 2;
Comparer(a, 1)
.and(b, 2)
.then(log("pass"))
.otherwise(log("fail"));
Your question is sort of pointless, but you could use the multiplication operator *
instead of &&
:
if(a==1 * b==2){
//do something
}
b
is an expression that changes state, for instance a function call, then the result may be far from equal.
b
trigger a change of state? Go ahead, I'll wait.
Dec 8, 2012 at 17:04
If you want to implement the operator &&
as a function, it's going to get ugly, because you need to pass the conditions as closures, whenever you care about short-circuiting with side-effects.
Example:
if(condition && changeTheWorld()) { ... }
// Cannot be translated into a function call of this nature:
if(land(condition, changeTheWorld()) { ... }
Instead, you would need to create a closure:
if(land(condition, function() {return changeTheWorld()}) {...}
As you can see, it's really cumbersome and verbose while there's no advantage.
If you really need this as a funciton, here is an
This function emulates the semantics of &&
correctly, if you pass the conditions that must not get evaluated—in the case of short-circuiting—as functions instead as expressions.
In other words, the function goes through the arguments in order, if one is a function, it evaluates it first, otherwise it just takes its value, if falsy, it aborts by returning false, otherwise, it continues with the next parameter.
function land(){
for(var i = 0; i < arguments.length; i++) {
var operand = arguments[i];
var value = (typeof operand === 'function') ? operand() : operand;
if (!value) {
return false;
}
}
return true;
}
Example:
function evaluateTo(result) {
return function() {
console.log("Evaluating " + result);
return result;
};
}
if(land(true, evaluateTo(1))) {
console.log("All truthy");
}
// Outputs:
// Evaluating 1
// All truthy
if(land(evaluateTo(1), evaluateTo(0), evaluateTo(true))) {
console.log("All truthy");
}
// Outputs:
// Evaluating 1
// Evaluating 0
Obligatory missile example
function changeTheWorld() {
console.log("Missiles away!");
// Firing 3 missiles
return nukeTheGlobe(3);
}
if(false && changeTheWorld() == 3) { ... }
// we survived, missiles not fired
if(naiveLand(maybe, changeTheWorld() == 3) { ... }
// Missiles away! no matter what value `maybe` has
if(land(false, function(){ return changeTheWorld() == 3; })) {...}
// we survived, missiles not fired
changeTheWorld()
is symbolic for any expression, that changes some state when evaluated. If condition
before the &&
operator evaluates to false, that state is not going to be changed. Imagine changeTheWorld()
deletes a comment and condition
is used to confirm the action. Without closures, the comment is going to be deleted no matter whether the deletion was confirmed or not.
&&
as a function naively and entirely symbolic because of that. Moreover, it is about how you would use the function if you did correctly replicate the semantics of &&
and how it's going to turn out. An implementation of land
is not necessary to understand my answer.
&&
, just adding more overhead.!(a != 1 || b != 2)
:)