0
function jjj(asi) {
  asi=3;
}

jjj();
console.log(asi);

Here I am thinking that asi is a global variable but while running this code it is giving that asi is not defined.

As per the books and official doc I have studied that if you mention the variable name without the keyword var then it becomes global so I think same rule applies to asi variable also

3
  • 2
    You’re not defining a global variable. You’re redefining the parameter of the function. Commented Feb 28, 2018 at 5:36
  • @xufox I think that parameter is variable which we can use in our other parts of the code Commented Feb 28, 2018 at 5:37
  • 2
    You mean using the parameter asi outside of the function? No, this is definitely not possible. Commented Feb 28, 2018 at 5:39

2 Answers 2

2

here I am thinking that asi is a global variable but while running this code it is giving that asi is not defined

It would be creating an implicit global if you weren't declaring it as a parameter instead, e.g.:

function jjj() {
//           ^---------- removed `asi` here
  asi = 3;
}
jjj();
console.log(asi);

Note that implicit globals are a really bad idea (I called my blog post on them The Horror of Implicit Globals for a reason) and you should use strict mode to make them the errors they always should have been:

"use strict";
function jjj() {
  asi = 3; // ReferenceError: asi is not defined
}
jjj();
console.log(asi);

Sign up to request clarification or add additional context in comments.

Comments

1

In your case the function argument is reassigned with a new value.

function jjj(asi) {
  asi = 3 // the function argument will have new value
  mno = 4 // this will be a global variable
}
jjj();
console.log(asi);
console.log(mno);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.