Can I use the parameters of a function outside the function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can I use the parameters of a function outside the function?

function sayHello(name, age) { document.write( name + " is " + age + " years old."); } can the variables name and age be used later? var anyName = name; is this valid?

17th Jun 2018, 5:20 PM
Manoj
Manoj - avatar
4 Answers
+ 1
Nope, those parameters are private only to that function.
17th Jun 2018, 5:42 PM
Andre Daniel
Andre Daniel - avatar
+ 1
What if I initialize the variable name before the function is defined? var name = "Jonny"; function.........
17th Jun 2018, 5:50 PM
Manoj
Manoj - avatar
+ 1
Thanks. I got it now. I need to go deeper into "scope of variables".
18th Jun 2018, 5:08 PM
Manoj
Manoj - avatar
0
yes, im no js coder but i know a little bit, and i think this is possible: function sayHello(name,age){ documwnt.write(name + "is" + age + "years old"); } let myName = "manoj"; let myAge = 16; SayHello(myName,myAge); document.write(myName); document.write(myAge); OUTPUT: manoj is 16 years old manoj 16 so now you used the vars myName and myAge outside the function by declaring the var not in the parameter but befor the function
21st Jun 2018, 6:10 AM
Cat Sauce
Cat Sauce - avatar