9
Can I "access" a specific parameter of a Javasscript function, that is, give a value for a given parameter? As an example is better than text (since I don’t know how to explain myself properly), here’s a:
In Python, having a function:
def funcX(A="A",B="B",C="C"):
print(A+B+C)
...that would result in:
'ABC'
...I can change the value of a given parameter like this:
funcX(C = "c")
...which results in:
'ABc'
I want to know if there is any way to do the same in Javascript. Example:
function funcX(A,B,C){
if(typeof A != 'string'){A= "A"}
if(typeof B != 'string'){B= "B"}
if(typeof C != 'string'){C= "C"}
alert(A+B+C)
};
Would result in:
'ABC'
And then I’d like to do something like:
funcX(C = "c")
...to result in:
'ABc'
Taking this opportunity, I would like to ask a parallel question (if this is allowed):
There is a better way to give "default values" for the parameters of Javascript functions than if(typeof A != 'string'){A= "A"}
?
As in Python def funcX(A="A")
*(sorry so much comparison, it’s just that I learned to code in Python, and it’s the only reference I have)
Thanks Sergio, I wish I could save typing, but unfortunately I can not pass only one parameter. Do what At least now I don’t have a doubt, I know I can’t ?
– Jonatas Amaral