learning-zone / javascript-basics

JavaScript Basics
https://learning-zone.github.io/javascript-basics/
1.75k stars 616 forks source link

issues in questions 8 & 297... and its answers #18

Closed Adam-Books closed 2 years ago

Adam-Books commented 2 years ago

8: What are global variables?

Your answer is: Global variables are those that are available throughout the length of the code without any scope. The var keyword is used to declare a local variable but if you omit it then it will become a global variable

msg = "Hello"  // var is missing, it becomes global variable

The problem with global variables is the conflict of variable names of local and global scope. It is also difficult to debug and test the code that relies on global variables.


But I think that:

Global variables are those that are available throughout the length of the code without any scope. The var keyword is used to declare declares a function-scoped or globally-scoped variable but if you omit it then it will become a global variable only.

msg = "Hello"  // var is missing, it becomes a global variable

But var in the 'Block scope' is still a global variable:

var x = 1;

if (x === 1) {
  var x = 2;

  console.log(x);
  // expected output: 2
}

console.log(x);
// expected output: 2

297: In JavaScript, What is the difference between var x = 1 and x = 1?

Your Answer is:

‘var x = 1’ will create a variable within the current scope. Given this is declared in a function, x will not be available outside it unless explicitly returned.

‘x = 1’ will create a variable within the global scope. Thus, any other code can access and alter its value. It is generally a bad practice to use variables in a global scope.


But... I think that this is a wrong answer, Because in MDN :

The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.

and

in strict mode: 'x = 1' is not allowed.

So I hope you correct it.

Because JavaScript has 3 types of scope:

learning-zone commented 2 years ago

Any reference provided for above definition will be appreciated.

Adam-Books commented 2 years ago

References Links:

- MDN

- W3schools

learning-zone commented 2 years ago

If you can justify your statement with provided reference will be appreciated.

Adam-Books commented 2 years ago

Q 8: What are global variables?

I suggest this answer:

Global variables are declared outside of a function or declared with a window object for accessibility throughout the program (unless shadowed by locals). If you declare a variable without using var, even if it’s inside a function, it will still be seen as global:


var x = 5; // global
function someThing(y) {
  var z = x + y;
  console.log(z);
}
someThing(4); // 9
console.log(x); // 5

Note:

Using Undeclared Variables

var x = 5; // global
function someThing(y) {
  x = 1; // still global!
  var z = x + y;
  console.log(z);
}
someThing(4) // 5
console.log(x) // 1
var x = 5; // global
function someThing(y) {
  var x = 3; // local
  var z = x + y;
  console.log(z);
}
someThing(4); // 7
console.log(x); // 5

A global variable is also an object of the current scope, such as the browser window:

var dog = “Fluffy”;
console.log(dog); // Fluffy;

var dog = “Fluffy”;
console.log(window.dog); // Fluffy

To declare JavaScript global variables inside the function, you need to use a window object. For example:

window.value=90;

Now it can be declared inside any function and can be accessed from any function. For example:

function m(){  
window.value=100;  //declaring global variable by window object  
}  
function n(){  
console.log(window.value);  //accessing global variable from other function  
}  

It’s a best practice to minimize global variables. Since the variable can be accessed anywhere in the program, they can cause strange behavior.

References:

Adam-Books commented 2 years ago

Q 297: In JavaScript, What is the difference between var x = 1 and x = 1?

I suggest this answer:

var x = 1:

var x = 1;

if (x === 1) {
  var x = 2;

  console.log(x);
  // expected output: 2
}

console.log(x);
// expected output: 2
var x = 5; // global
function someThing(y) {
  var x = 3; // local
  var z = x + y;
  console.log(z);
}
someThing(4); // 7
console.log(x); // 5

x = 1:

var x = 5; // global
function someThing(y) {
  x = 1; // still global!
  var z = x + y;
  console.log(z);
}
someThing(4) // 5
console.log(x) // 1

And this example explains the difference between hoisting var x = 1 and x = 1 (in all the types of JavaScript's scopes: [Block scope - Function scope - Global scope])

{
  console.log(x + y); // NaN
  var x = 1;
  var y = 2;
}
{
  console.log(x + y); // Uncaught ReferenceError: x is not defined
   x = 1;
   y = 2;
}


var x = 1 x = 1
Strict mode
Block scope
Function scope
Global scope
Hoisting
Reassigning


References:

learning-zone commented 2 years ago

Please create a PR, so that repository can be updated.

Adam-Books commented 2 years ago

OK... happy new year ☺

learning-zone commented 2 years ago

Happy New Year 🎉 😄