This chapter describes how to write statements using variables, which store values like numbers and words, and operators, which are symbols that perform a computation.
Semantically, for both prefix and postfix ++ the value of the variable gets incremented by 1; if the ++ is written before the variable (a prefix operator), the result of the operator is the NEW/CURRENT value stored in the variable; if the ++ is written after the variable (a postfix operator), the result of the operator ...
You cannot store an operator in JavaScript like you have requested. You can store a function to a variable and use that instead. Sorry, but JavaScript does not allow this operator overloading.
Assignment Operators are used to assign a value to a property or variable. Assignment Operators can be numeric, date, system, time, or text. Comparison Operators are used to perform comparisons. Concatenation Operators are used to combine strings.
Not out of the box. However, it's easy to build by hand in many languages including JS.
var operators = {
'+': function(a, b) { return a + b },
'<': function(a, b) { return a < b },
// ...
};
var op = '+';
alert(operators[op](10, 20));
You can use ascii-based names like plus
, to avoid going through strings if you don't need to. However, half of the questions similar to this one were asked because someone had strings representing operators and wanted functions from them.
I believe you want a variable operator. here's one, created as object. you can change the current operation by changing:
[yourObjectName].operation = "<" //changes operation to less than
function VarOperator(op) { //you object containing your operator
this.operation = op;
this.evaluate = function evaluate(param1, param2) {
switch(this.operation) {
case "+":
return param1 + param2;
case "-":
return param1 - param2;
case "*":
return param1 * param2;
case "/":
return param1 / param2;
case "<":
return param1 < param2;
case ">":
return param1 > param2;
}
}
}
//sample usage:
var vo = new VarOperator("+"); //initial operation: addition
vo.evaluate(21,5); // returns 26
vo.operation = "-" // new operation: subtraction
vo.evaluate(21,5); //returns 16
vo.operation = ">" //new operation: ">"
vo.evaluate(21,5); //returns true
You can use the eval()
function, but that is not a good idea.
I think the better way is writing functions for your operators like this:
var addition = function(first, second) {
return first+second;
};
var subtraction = function(first, second) {
return first-second;
};
var operator = addition;
alert(operator(12, 13));
var operator = subtraction;
alert(operator(12, 13));
We can implement this using eval
, since we are using it for operator checking.
var number1 = 30;
var number2 = 40;
var operator = '===';
function evaluate(param1, param2, operator) {
return eval(param1 + operator + param2);
}
if (evaluate(number1, number2, operator)) {}
in this way we can use dynamic operator evaluation.
You can't overload operators in JavaScript. You can off course use functions to help
var plus = function(a, b) {
return a + b;
};
var smaller = function(a, b) {
return a < b;
};
var operator = plus;
var total = operator(a, b);
operator = smaller;
if(operator(var1, var2)){ /*do something*/ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With