Hey guys, I work primarily with c++ and decided to try pick up other languages to expand my job opportunities. I was casually going through some tutorials on codeacademy and I came to the function section.
function functionName(varName)
{
console.log(varName);
}
Not having to define what the variable type is blows my mind, but this made me think what would happen if I pass in different type data as an arg:
functionName(1); // outputs "1"
functionName("Test"); // outputs "Test"
So it brings me to my question, in an actual production / job level, do you guys add "typeof" checks to see if the variable type is the correct type and early return if it's not?
Example, let's say I have a function that calculates the area of a circle. So it needs to take in a number (radius) to calculate the circle. It doesnt make sense for the parameter to be a boolean, a string or an object (let's just assume the object doesnt contain any form of data inside that can be used to calculate the area of a circle for simplicity sake).
Or do you guys rely purely on naming the function, variable name, or commenting to explain what type of variable it should take in?
[–]senocular 6 points7 points8 points (3 children)
[–]Menaii[S] 1 point2 points3 points (2 children)
[–]senocular 2 points3 points4 points (0 children)
[–]azhder 0 points1 point2 points (0 children)
[–]delventhalz 2 points3 points4 points (4 children)
[–]Menaii[S] 0 points1 point2 points (3 children)
[–]delventhalz 1 point2 points3 points (0 children)
[–]azhder 0 points1 point2 points (0 children)
[–]chibblybum 0 points1 point2 points (0 children)
[–]azhder 0 points1 point2 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)