how to pass eternal varibles into javascript in javascript

To pass external variables into JavaScript, you can use Global variables or define the variables in external modules.

Here is an example of using global variables in JavaScript:

index.tsx
//define global variables
var var1 = "Hello";
var var2 = "World";

//function that uses global variables
function greet() {
   console.log(var1 + " " + var2);
}

//call the function
greet();
191 chars
12 lines

Another way to pass external variables into JavaScript is by using modules:

index.tsx
//example module
let myModule = (function() {

  //private variables
  let var1 = "Hello";
  let var2 = "World";

  //private function
  let greet = function() {
    console.log(var1 + " " + var2);
  }

  //public function
  return {
    sayHello: function() {
      greet();
    }
  }
})();

//call the public function
myModule.sayHello();
341 chars
23 lines

In this example, we define a module with private variables and functions. We then expose a public function that calls the private greet() function, which uses the private variables. To use this module, we just call myModule.sayHello().

gistlibby LogSnag