Saw some JS notation today that I’ve never seen before, bear with me if it is something common that everyone knows.
var cookiepath = cookiepath || '';
Now, is this just saying, if a variable named cookiepath already exists, set it to cookiepath or if it doesn’t, set it to ”?
The
cookiepathvariable is being declared, an initialized.The
varstatement doesn’t make any harm if an identifier was already declared in the current lexical scope.If
cookiepathwasn’t declared yet, thevarstatement, before run-time, will initialize the variable toundefined.After that, in run-time, the assignment is made, if it’s value is falsy (other than
null,undefined, an empty string,0,NaN, orfalse) it’s set to an empty string.Keep in mind that you have access to the
cookiepathvariable in the local scope.Consider the following example:
In the above example, we have a global
cookiepathvariable, on the global scope, but when the function is executed, a localcookiepathvariable will be declared on the scope of the function, and this shadows the value of the outer scope, and this behavior is noticeable even before thevarstatement in the function, e.g.: