Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's correct: var declaration if undefined

Tags:

javascript

I have heard different opinions about the usage of var at a situation like this:

function(maybeUndefined) {
    if(typeof maybeUndefined === 'undefined')
        var maybeUndefined = 'bob';
}

Is it necessary to notate var, or not, because maybeUndefined is an argument of function?

like image 901
RienNeVaPlu͢s Avatar asked Dec 01 '22 03:12

RienNeVaPlu͢s


1 Answers

You do not need the var in this case, as mayBeUndefined is already allocated within the scope of the function (hint: listing argument variables in a function definition causes those variables to be declared locally). That var is therefore completely optional, though completely pointless (and a drain on readability).

like image 191
Sébastien Renauld Avatar answered Dec 04 '22 10:12

Sébastien Renauld