Basic Js question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Basic Js question

Hello guys, I have a Js question that seems to be basic but I cannot figure it out, please help me. function print(f) { let first = 'Mary'; console.log(f)} When I executed it with a variable declared in the function scope as argument, I got an error. print(first); //--> first is not defined Why is this happening? I thought I should be able to access the print scope since I am executing the print function.

29th Mar 2019, 9:47 AM
MeiCC
MeiCC - avatar
3 Answers
+ 1
I understand it, just being curious about why can the variable inside the function not be accessed.
29th Mar 2019, 11:19 AM
MeiCC
MeiCC - avatar
+ 6
No, because you have no variable named "first" outside the function, You have to place the "first" outside the function
29th Mar 2019, 10:32 AM
Jingga Sona
Jingga Sona - avatar
+ 2
hold on.. you did not call the function first. second you passing f but where is f in your function. when you pass argument you don't have to declare it again because it ia already declared. https://code.sololearn.com/WsBfDZ4ZT8t7/?ref=app
29th Mar 2019, 12:04 PM
Ahmad Ali
Ahmad Ali - avatar