-1

I have one global variable in one JavaScript file and I using it in another JavaScript file.

but sometime we not use that JavaScript file(where we define Global variable) so I am getting error like var_Name is not defined error. So any way in JavaScript to check variable is defined or not and if its not defined than how to defined that variable run time.

var_Global?var_Abc=var_Global:var_Global=somevalue

when if global variable is defined than I want to assign that variable to another variable and if not than I want to define that variable.

Thanks.

Pravin Tukadiya
  • 489
  • 4
  • 20
  • 5
    literally do that? "if ([typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) varname === "undefined") { console.log("varname is not defined"); }" – Mike 'Pomax' Kamermans May 18 '17 at 05:38
  • down voter please let me know what I have done wrong in this question so I can improve in next time.Thanks – Pravin Tukadiya May 18 '17 at 06:01
  • 1
    @PravinTukadiya I'm not the one who down-voted the question, but my guess would be that it is because there most probably is a blog post or SO question about this AKA that this question is on google for a long time. – Artūrs Lataks May 18 '17 at 06:26

3 Answers3

0

file1.js

var x = 'foo';

file2.js

if (typeof x !== 'undefined') { 
    console.log('x is defined') 
} else { 
    console.log('x is undefined')
    //define x
    x = 'foo'
}
0

Do it like this:

file1.js

var x = 30;

file2.js

var x = x || 10;

This is a common technique to check for a variable value in such contexts, specially for non-strict mode. So x || 10 will first see if x has any value? In case x is undefined due to non-inclusion of the the file, then it would get a value 10. If x was defined, it would take value from file1.js. You can change this value 10 to any value you want.

Pankaj Shukla
  • 2,657
  • 2
  • 11
  • 18
  • Your code in `file2.js` will result in a `ReferenceError` in strict mode. Better to use `typeof`. –  May 18 '17 at 05:58
  • getting error x is not defined. – Pravin Tukadiya May 18 '17 at 05:59
  • @PravinTukadiya Are you using strict mode? – Pankaj Shukla May 18 '17 at 06:00
  • no I am using in the ext js – Pravin Tukadiya May 18 '17 at 06:01
  • Why downvote? OP hasn't specified that he is using strict mode! – Pankaj Shukla May 18 '17 at 06:08
  • Not sure why you think there is a downvote. Anyway, the OP does not need to mention that he is using strict mode. You should post code which runs in strict mode. For instance, if the code is being transpiled by Babel or TypeScript, it is likely that it is being transpiled into strict mode. Your code also has the problem that it will replace any non-truthy value of `x` (`false`, `0`, `""`, `null`) with 10. –  May 18 '17 at 06:21
  • @PravinTukadiya To be fair, code in file2.js will fail in non-strict mode as well, you would need to add var there, like this: `var x = x || 10;` – Artūrs Lataks May 18 '17 at 06:29
  • @torazaburo There was one earlier! Also, point taken but in this case he is using extjs and extjs framework itself doesn't play nicely with strict mode unless you make arrangements. So the solution was with context and not generic. – Pankaj Shukla May 18 '17 at 06:33
  • @ArtūrsLataks Sorry, my bad. You are right. – Pankaj Shukla May 18 '17 at 06:44
0

Try below ways :

1. check for undefined

if (window.var_Global === undefined) {
    window.var_Global = 'hi';
}

2. check for property

if (!window.hasOwnProperty('var_Global')) {
    window.var_Global = 'hi';
}

3. using logical OR (||) operator

window.var_Global = window.var_Global || 'hi';

4. To check all the Global variables.

Object.keys( window ); // return array of all the global variables.
Debug Diva
  • 26,058
  • 13
  • 70
  • 123