javascript make variables inside a function global

javascript global variables
how to access variable outside function in javascript
change global variable in function javascript
nodejs global variable
javascript global variable across files
jquery global variable
javascript function
javascript global variable undefined

I'm new to Java Script and here's what I'm stuck with. I've been trying to make a variable inside my function global so that I could use it in the other parts of the code. Nothing seems to be working so far. Below is my code:

var json2="-";

var request = require("request");
var smallpie1 = 
"https://s3.amazonaws.com/vecareclientjson/user1/predictions.json";

var pre = {rejectUnauthorized: false,
       url: smallpie1,
       method: 'GET',
       json: true
};
function test1(){
    request(pre,function (error,response,body){
        json2 = JSON.stringify(body);
        console.log(json2);
    });
};
console.log(json2);

Output:
-
[Done] exited with code=0 in 0.231 seconds

I was expecting the content in the json to overwrite the json2 object. The goal is to make the json2 object inside the function test1() global.

As other contributers are telling you, you must run the test1() function. You can do so by adding it to the bottom of your code, before logging the json2 variable, such as:

var json2="-";

var request = require("request");
var smallpie1 = 
"https://s3.amazonaws.com/vecareclientjson/user1/predictions.json";

var pre = {rejectUnauthorized: false,
       url: smallpie1,
       method: 'GET',
       json: true
};
function test1(){
    request(pre,function (error,response,body){
        json2 = JSON.stringify(body);
        console.log(json2);
    });
};

test1(); // Here you call the function, which will modify json2
console.log(json2); // Here you print json2 current value

JavaScript Scope, 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  Variables that are created inside a function are local variables, and local variables, and local variables can only be referred to by the code within the function. Variables created outside of functions are global variables, and the code in all functions have access to all global variables.

It seems that you have not yet run the function.

Define global variable in a JavaScript function, How will you set a global variable inside a function? 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 global variable in JavaScript. <script>. var value=50;//global variable. function a () {. alert (value); }

Run the function test1 before console.

Global keyword in Python, scope: All scripts and functions on a web page can access it. They are deleted when the browser window is closed but is available to other pages loaded on the same window. There are two ways to declare a variable globally: Declare a variable outside the functions. Assign value to a variable inside a function without declaring it using “var” keyword. filter_none. edit.

Scope in JavaScript, How many ways we can declare global variable in JavaScript? For one, you can declare the variable(s) in the global space (preferably in a closure) but not assign it a value until you've retrieved that information from the user. Example: ( function (){ var infoINeed ; function thingToDo () { infoINeed = "whatever" ; } thingToDo (); // then you can use it down here, too function anotherThing ( infoINeed ) { // do something with infoINeed value

How to change the value of a global variable inside of a function , Pre-requisite: Global and Local variables in JavaScript. Local Scope: Variables which are declared inside a function is called local variables and these are  But, because JavaScript allows re-declaration of same variables, it can easily become a problem if a function is using a variable with same name as global variable in its scope. Normally accessing this variable's value will return the value of the current scope variable. Here is what we are talking about.

Global and Local variables in JavaScript, Local Variable: Since local variables are defined inside the function so pass data to a function and return data from it, global variables often create problems. In JavaScript there are two types of scope: Local scope. Global scope. JavaScript has function scope: Each function creates a new scope. Scope determines the accessibility (visibility) of these variables. Variables defined inside a function are not accessible (visible) from outside the function.

Comments
  • Do you ever run test1() ?