JavaScript global variables & self-invoking anonymous functions

javascript global variables best practice
javascript global variable undefined
javascript global variable across files
change global variable in function javascript
javascript global keyword
javascript global variable not working
nodejs global variable
how to access variable outside function in javascript

So I've been reading through Javascript - The Good Parts and one thing that Crockford points out is the use weakness of global variables in Javascript, in such a way that if your product is expanded in some manner, and it relies on a 'global' variable it could be inadvertently set.

That's all good and fine and I understand the pros/cons of protecting variables, in other manners such as closures as well. However, I was doing some thinking, and wrapping code in a function like so:

(function () {
    var x = 'meh';
})();
(function () {
    alert(typeof x); // undefined
})();

gives it variable scope, which thereby prevents cross contamination of variables. I'm not sure if there's a blatant downside to this approach though and wondered if the community had any input, or if I'm just overthinking things and ignoring the main point.

That's a perfectly legal way of doing things -- the variables inside of your function (as long as they are prefaced by var) are local to the function. It's called the module pattern, and it's very well accepted.

How and why to avoid global variables in JavaScript?, A JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function. Let's see the simple example of  A JavaScript global variable is declared outside the function or declared with window object. It can be accessed from any function. It can be accessed from any function. Let’s see the simple example of global variable in JavaScript.

To create applications with javascript, you must attempt to keep variables in a local scope, and anything inside a namespace. It's a good pratice and prevent a serie of harm codes and unespected behaviors.

read this

it's a article talking about the vantages of doing that.

Local, Global and Static variables in C, A global variable has global scope which means it can be defined anywhere in your JavaScript code. Within the body of a function, a local variable takes precedence over a global variable with the same name. A global variable has global scope which means it can be defined anywhere in your JavaScript code. Within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable.

Making it a global function is not the answer. Why wouldn't you do this? This keeps x out of the global namespace.

(function () {
    var x = 'meh';
    alert(typeof x);  //string
})();

JavaScript global variable, Yes, as the others have said, you can use var at global scope (outside of all functions) to declare a global variable: <script> var yourGlobalVariable; function  You can implicitly create a global variable, or create a window property inside a function, but you can't define a global variable inside a function. – Guffa Nov 17 '13 at 13:22. 1. With regards to JavaScript in a browser environment, a global variable and a property of window are synonymous.

(function (global) {
    global.x = 'meh';
})(window);
(function () {
    alert(typeof x); // string
})();

What is the correct way to define global variables in JavaScript?, In contrast, global variables are variables that are defined outside of functions. These variables have global scope, so they can be used by any function without​  JavaScript uses functions to manage scope. A variable defined inside a function are in the local scope and not available outside the function. While a variable defined outside of a function is in

Define global variable in a JavaScript function, JavaScript has two scopes – global and local. Any variable declared outside of a function belongs to the global scope, and is therefore accessible from anywhere in your code. Each function has its own scope, and any variable declared within that function is only accessible from that function and any nested functions. Global variables are actually the default in JavaScript. The keyword var is actually what causes the variable you are assigning to be placed in the local scope. If you want to declare a variable in the global scope from within any function, just omit the var keyword:

Global and Local variables in JavaScript, In a browser, global functions and variables declared with var (not let/const !) become the property of the global object: var gVar = 5 ; alert  JavaScript variables are containers for storing data values. In this example, x, y, and z, are variables: Example. var x = 5; var y = 6; var z = x + y; Try it Yourself ». From the example above, you can expect: x stores the value 5.

Demystifying JavaScript Variable Scope and Hoisting, Global Variables in JavaScript Explained. Global variables are declared outside of a function for accessibility throughout the program, while  Assign that lone global variable a name that no one else is likely to choose, such as your name or employer's name or best-of-all, a domain name that belongs you or your employer. Another, more common way to handle this problem is to take advantage of of the way that JavaScript handles variable scope within functions.

Comments
  • Awesome. Appreciate the link. I wonder why this isn't really mentioned in his book (so far, it's possible it is later, but it'd make sense to have it in the part bashing global variables). At least I know I'm not being overly analytical for once.
  • This is my nit-picking day, apparently, but those things are not called closures in a strict sense (in a general computer science sense, all JavaScript functions are closures): those self-invoking anonymous functions would only be closures if they are used outside of the scope in which they were defined (e.g., when attaching an anonymous function to an event listener within a function that exits before the listener is detached).
  • @Marcel -- thanks, I've updated it to use function rather than closure.
  • The point in the example was to get undefined, therefore isolation from eachother.
  • Please try to avoid code only answers and add some detail or explanation. Thanks