Default value of a type in javascript-Collection of common programming errors
I don’t understand why do you ask such a question. Anyways, the default value of a variable in JavaScript is null
or undefined
.
For learning purposes, I took this from WikiBooks JavaScript/Variables and Types:
Variables are commonly explicitly declared by the var statement, as shown below:
var c;
The above variable is created, but has the default value of
undefined
. To be of value, the variable needs to be initialized:var c = 0;
After being declared, a variable may be assigned a new value which will replace the old one:
c = 1;
But make sure to declare a variable with var before (or while) assigning to it; otherwise you will create a “scope bug.”
And now, when you ask why the value is not 0
, it is because, even null
or undefined
are values, which are not defined. Not defined is different from being defined and empty. It might return 0
, when the value is declared and not defined.
Originally posted 2013-11-09 20:50:38.