In JavaScript, how come you are able to pass no arguments to a function that “requires” arguments?
For example:
function printName(name) {
if (name) console.log(name);
else console.log("No name");
}
printName();
However, if you try the same thing in Python you get an error:
def foo(name):
if name: print(name)
else: print("No name")
foo()
TypeError: foo() missing 1 required positional argument: 'name'
Original exception was:
Traceback (most recent call last):
File "random.py", line 5, in <module>
foo()
TypeError: foo() missing 1 required positional argument: 'name'
I can tell this relates to the undefined
keyword: if you console.log()
nothing, it logs undefined
.
Can somebody explain what is going on when it does this/how this is allowed to work in relation to other languages that don’t allow it, such as Python?