+ 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?
4 Answers
+ 1
Nope, those parameters are private only to that function.
+ 1
What if I initialize the variable name before the function is defined?
var name = "Jonny";
function.........
+ 1
Thanks. I got it now.
I need to go deeper into "scope of variables".
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