Default Values
<h2>Default Values</h2> <p>We know how to declare a <a href="/courses/video/5/157/Using-Variables.html">variable</a> and assign a value to it. Have you ever wondered what happens if we declare a variable and don't assign any value to it? In this case, the application has to reserve memory for the variable, irrespective of whether the value is assigned or not. The memory size depends upon the <a href="/courses/video/5/161/Numbers-int-And-unit.html">type of variable</a> declared. This memory location will be occupied by the value we assign later. If we don't assign any value and proceed to the next command, the compiler saves a default value for that memory space. All such values assigned by the application are termed as default values.</p> <h2>Default values for different <a href="/courses/video/5/161/Numbers-int-And-unit.html">data types</a></h2> <p>Let's see how the application assigns values for different types of data. When we declare a variable of type <a href="/courses/video/4/154/Strings-As-Our-First-Variable.html">string</a>, for example var str:String; the default value for this variable is <a href="/courses/video/7/60/Null-vs-Undefined.html">nothing -- or null</a>. Since we didn't assign a value to str, no memory is assigned to it. Similarly with a <a href="/courses/video/4/156/Numbers-As-Our-Second-Variable.html">Number</a>, the default value that is assigned is NAN (not a number). But an int has a set of values: Z= {...-3.-2,-1, 0, 1, 2,....} and <a href="/courses/video/5/161/Numbers-int-And-unit.html">uint</a> has all the positive integers {0, 1, 2, ...}. For these data types, the empty value or the default value is zero. Finally, for a <a href="/courses/video/5/162/Boolean.html">Boolean</a> data type, there are only two possible values: true or false. The value closest to nothing in this case is false. It's not exactly equal to null, but the nearest value to nothing among the two is definitely false. So, the application assigns false as the Boolean variable default.</p> <h2>Assign values to all the variables manually</h2> <p>When we trace these <a href="/courses/video/5/157/Using-Variables.html">variables</a>, we can find the above mentioned default values in the output panel. But when we depend on defaults (lazy behavior!) in big programs, this might cause confusion. So to avoid any difficulty, we must always assign a value to the variable soon after it's declared. Even if we want the variable to be empty, we should assign some value which is considered as <a href="/courses/video/7/60/Null-vs-Undefined.html">nothing</a>. For example, assign 0 to an <a href="/courses/video/5/161/Numbers-int-And-unit.html">int or uint</a>, instead of asking the compiler to assign the default values. There's a good reason for this: various applications have their own default values and we might get confused when we switch to another application. To be on the safe side, we should always assign a value to a variable manually as soon as it is declared.</p>