return type javascript code example
Example 1: how to get type of variable in javascript
> typeof "foo"
"string"
> typeof true
"boolean"
> typeof 42
"number"
Example 2: 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"));
Example 3: return string from javascript function
<script>
function showName() {
var result;
result = addString('Hello', ' World');
document.write (result );
}
function addString(fName, lName) {
var val;
val = fName + lName;
return val;
}
</script>
<input type = "button" onclick = "showName()" value = "Result">
Example 4: what does return do in javascript
function add(a, b) {
return a + b;
}
var sum = add(5, 24);