function as object javascript code example
Example 1: function inside object javascript
var obj = {
func: function(a, b) {
return a * b;
}
};
obj.func(3, 6);
Example 2: how to define function in object javascript
let example = {
calc: function (num1, num2) {
return num1 + num2;
},
};
example.calc(1,2)
Example 3: js function
function myfunction() {
console.log("function");
};
myfunction()
function calculate(x, y, op) {
var answer;
if ( op = "add" ) {
answer = x + y;
};
else if ( op = "sub" ) {
answer = x - y;
};
else if ( op = "multi" ) {
answer = x * y;
};
else if ( op = "divide" ) {
answer = x / y;
};
else {
answer = "Error";
};
return answer;
};
console.log(calculate(15, 3, "divide"));