Marcus Olsson
Marcus Olsson

Reputation: 2527

If not declared, declare the variable?

The topic might be a bit cryptic, but here's my problem (perhaps a n00b-thing?):

For my site I have a widget, that user can use on their site as such (for example):

<script type="text/javascript">
    var widget_width ="300";
</script>
<script src="http://something/a.js" type="text/javascript"></script>

Now, in the .js file I want to check if the user have declared the widget_width variable. So I thought I might do something like (I want the variable to be empty, "", because I do a server-side validation too):

if(typeof widget_width == 'undefined') {
    var widget_width = "";
}

This didn't work, neither did:

if(!widget_width) {
    var widget_width = "";
} 

Any ideas on how do declare a variable when needed this way?

Upvotes: 2

Views: 9319

Answers (5)

Laimonas
Laimonas

Reputation: 6001

This is probably beyond the scope of this question, but I wan't to emphasize difference between undeclared and undefined, as I found myself being confused it many times.

Please look at the following example.

<script type="text/javascript">
var a
/* checking if defined variable a has value */
/* warning: this can't be used to check if variable is declared,
or it will raise an error */
if(a === undefined) {
    //statement will execute
    console.log('variable a has no value')
} else { 
    //statement will not execute
    console.log('should not happen')
}

//checking if variable b is defined and has declared value

if(typeof b === 'undefined') {
    //statement will execute
    console.log('variable b has not been declared before')
} else {
    //statement will not execute
    console.log('should not happen')
}
</script>

I thing using it for such scenarios is safe. For those of You who think that undefined can be overwriten, according to MDN

Please note: In modern browsers (JavaScript 1.8.5 / Firefox 4+), undefined is a non-configurable, non-writable property per the ECMAScript 5 specification. Even when this is not the case, avoid overriding it.

Upvotes: 1

Hitesh Prajapati
Hitesh Prajapati

Reputation: 2842

I think you use below code as follow.

 <script type="text/javascript">
    var widget_width = "300";
    TTTTTTT();
 </script>

 and other your js file

 function TTTTTTT() {

  if (typeof widget_width == 'undefined') 
  {
    alert("calling inner");    
    var widget_width = "";
  }
 }

It's work....

Upvotes: 0

Jano
Jano

Reputation: 181

The Javascript has scoping rules in play - so when a variable is declared depending on the context where the declaration is made (if within function, it will be declared in the current scope) the variable is declared there.

By using a trick you can get variable declared in global scope if you leave out the 'var' keyword.

What would happen is that JS interpreter will try to find this variable in current scope (so local, within the function), and because it cannot find it, it will try to search all parent/enclosing scopes until it reaches Global (last) scope.

If the variable is not there either (and we know it is not due to the check you're making), it will create it in the last scope searched, in this case, global.

So, remove the 'var' keyword and you'll be fine...

Upvotes: 4

Misam
Misam

Reputation: 4389

try with this

if(widget_width == undefined)
{ 
   var widget_width = "";
}

Upvotes: -1

neeebzz
neeebzz

Reputation: 11538

remove the var. make it >

if(typeof widget_width == 'undefined') {
    widget_width = "";
}

Upvotes: 14

Related Questions