![]() The first reason is that when you create a global variable, it exists throughout the lifetime of the application. I am going to strongly caution against creating global variables for a few very important reasons. Now, you might be thinking to yourself that you want to go off and create global variables now that you know more about them. It is also important to note that if you set the 'use strict' directive, Node will disable implicit globals and you will likely end up with an error at runtime rather than a working script. I strongly advise against using this method though as it is not the proper way to go about creating globals. This process essentially turns it into a globally accessible variable. If you declare a variable in a file without using the keyword var and then assign a value to it, the global object will set a property for this variable. The reason I have excluded this is because it is not a proper way of setting up a variable. ![]() What I have not talked about yet is another way that you can make a variable global. console.log(globalString) // Output: undefinedĬonsole.log(globalString) // Output: "We can change it too! " Global.js global.globalString = "This can be accessed anywhere!" Ĭonsole.log(globalString) // Output: "This can be accessed anywhere!"Ĭonsole.log(globalString) // Output: "Check me out now"Ĭonsole.log(globalString) // Output: undefined // Example.js // We can use the global we created in Global.js in this file // as well. ![]() Next, we change the value of globalString, and then finally we set it to undefined. In the code block below, we create a global variable called globalString and we give it a value. The global object is what gives us the scope of the entire project, rather than just the file (module) the variable was created in. To set up a global variable, we need to create it on the global object. Now that we have a better understanding of what a global variable in Node is, let's talk about how we actually set up and use a global variable. If you are not sure what a namespace is or you want to find out more about them, check out this article to learn more. You then declare a variable in this large container file, that variable now has scope throughout the whole program. To put it another way, imagine that your program is a giant file or container that has "imported" all the other JavaScript files. The reason this is possible is because JavaScript programs share a global namespace between all of the files in the program. When we talk about global variables, they exist for all of the files in a program meaning they have global scope for the program. I'm sure at this point you are wondering what this has to do with global variables. The variable fileScope can be changed or called from anywhere within this file, whereas the localScope variable only exists inside the function doSomething(). In the code snippet above, we can see that there are two variables, fileScope and localScope. This will result in an error because the variable does not exist // outside the function. Let localScope = "Can only be accessed inside this function" įileScope = "Can be accessed in the function too!" Scope.js let fileScope = "Can be accessed anywhere in the file" Now what do I mean by this? In the code below, you can see an example of two different variables with different scopes. When you create a variable in a JavaScript file, that variables only exists in the scope that it was declared in. The difference between a regular variable and a global variable comes down to their scope. Global variables can be initialized with a value, that value can be changed, and they can even be cleared out like a regular variable. Global variables are very similar, if not identical, to regular variables. This article will get you up and running in no time with everything you need to know about global variables. If you have never heard of global variables or worked with them, no need to worry. This article is aimed at developers who are at a beginner to intermediate skill level working with Node. Hey guys, in today's article I want to talk about global variables in Node.
0 Comments
Leave a Reply. |