const isString = (a, b, c) => {
if (((typeof a) == "string") && ((typeof b) == "string") && ((typeof c) == "string")) {
return "strings";
}
return "not strings";
}
Why is the => after the argument required in order for this to work? I got this test question correct via trial and error, but I don't understand why it doesn't work without =>.
Here are the instructions.
Create a function called "isString" that takes 3 arguments (x1, x2, x3)
- Check if each argument is a string using typeof.
- If each argument is a string, return "strings".
- If each argument is NOT a string, return "not strings".
The => is the syntax for an arrow function
The => operator is required because that is how javascript's syntax for anonymous functions works. This is creating a function equivilent to isString(a,b,c) {...}.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Assuming that this is javascript, the => is indicating that it is part of an Arrow Function. The following two codes are equivalent as for the understanding of the two.
// function declaration
function isString (a, b, c) {
if (((typeof a) == "string") && ((typeof b) == "string") && ((typeof c) == "string")) {
return "strings";
}
return "not strings";
}
// arrow function declaration; can also use `var` instead of `const`
const isString = (a, b, c) => {
if (((typeof a) == "string") && ((typeof b) == "string") && ((typeof c) == "string")) {
return "strings";
}
return "not strings";
};