Variable
Scope
In
many of programming languages we see that the scope of the variables
are block level. where as in JavaScript the scope of variables are
function level. Below is the Example.
Example
1:
if
(true) {
var
a = 10;
}
console.log('a
= ', a);
Result
a
= 10
Example
2:
function
: someFunction(){
var
a = 10;
}
console.log('a
= ', a);
Result
a
= undefined
Infinity
There
is a special value in JavaScript called Infinity. It represents a
number too big for JavaScript to handle.
Infinity
is indeed a number, as typing typeof Infinity in the console will
confirm.
Typeof
Infinity
number
Dividing
by 0 will give you infinity.
var
a = 6 / 0;
console.log(a);
Result
Infinity
there's
no practical value to it.
Infinity -
Infinity
NaN
-Infinity
+ Infinity
NaN
Any other arithmetic operation with Infinity as one of the operands will give you Infinity:
Infinity
- 10
Infinity
-Infinity
* 2
-Infinity
Infinity
/ 3
Infinity
Infinity
- 99999999999999999
Infinity
NaN
(Not A Number)
NaN
is a special value that is also a number
typeof NaN
"number"
var a =
NaN;
a
NaN
You
get NaN when you try to perform an operation that assumes
numbers but the operation fails. For example, if you try to multiply
10 by the character "f", the result is NaN,
var
a = 10 * "f";
a
NaN
Checking
if a Variable Exists
The
laziest way to do this is simply putting the variable in the
condition part of the if, for example if (somevar) {...}, but this is
not necessarily the best method. Let's take a look at an example that
tests whether a variable called somevar exists, and if so, sets the
result variable to yes:
var
result = '';
if
(somevar){result = 'yes';}
somevar
is not defined
Result
""
This
code obviously works, because at the end result was not "yes".
But firstly, the code generated a warning: somevar is not defined
and as a JavaScript whiz you don't want your code to do anything
like that. Secondly, just because if (somevar) returned false doesn't
mean that somevar is not defined. It could be that somevar is defined
and initialized but contains a falsy value, like false or 0.
A
better way to check if a variable is defined is to use typeof.
if
(typeof somevar !== "undefined"){result = 'yes';}
result;
""
typeof
will always return a string and you can compare this string with
"undefined". Note that the variable somevar may have been
declared but not assigned a value yet and you'll still get the same
result. So when testing with typeof like this, you're really testing
whether the variable has any value (other than the value undefined).
var
somevar;
if
(typeof somevar !== "undefined"){result = 'yes';}
result;
""
somevar
= undefined;
if
(typeof somevar !== "undefined"){result = 'yes';}
result;
""
If
a variable is defined and initialized with any value other than
undefined, its type returned by typeof is no longer "undefined".
somevar
= 123;
if
(typeof somevar !== "undefined"){result = 'yes';}
result;
"yes"
String
Conversions
When
you use a number-like string as an operand in an arithmetic
operation, the string is converted to a number behind the scenes.
(This works for all operations except addition, because of addition's
ambiguity)
var s = '1';
typeof
s
“String”
s
= 3 * s;
typeof
s;
"number"
s
3
var
s = '1'; s++; typeof s;
"number"
s
2
A
lazy way to convert any number-like string to a number is to multiply
it by 1 (a better way is to use a function called parseInt() )
If
the conversion fails, you'll get NaN:
var
d = '101 a';
d
* 1
NaN
A
lazy way to convert anything to a string is to concatenate it with an
empty string.
var
n = 1;
typeof
n;
"number"
n
= "" + n;
"1"
typeof
n;
"string"
Best Practice Tips
Minimize the
number of global variables.
Imagine two
people working on two different functions in the same script and they
both decide to use the same name for their global variable. This
could easily lead to unexpected results and hard-to-find bugs.
Always
declare your variables with the var statement.
Here's
an interesting example that shows an important aspect of the local
versus global scoping.
var
a = 123;
function
f() {
alert(a);
var
a = 1;
alert(a);
}
f();
The
first alert will show "undefined". This is
because inside the function the local scope is more important than
the global scope. So a local variable overwrites any global variable
with the same name. At the time of the first alert() a was not yet
defined (hence the value undefined) but it still existed in
the local space.