JavaScript

JavaScript: Variables

JavaScript: Variables

Before exploring JavaScript variables, let us understand JavaScript Data Types.

Most programming languages have a set of basic (standard) data types. Every program requires some sort of value to work on. These data values cannot be entered into the system (program) randomly, as the compiler won't be able to interpret them. Data types are the predefined types of values that are supported by the programming language, manipulation of these predefined values can be done to achieve the required results.

There are three types of data types that can be worked on in JavaScript programming language:

  • First are Numbers; you can use numbers according to your requirements. Examples: 34, 89.45, etc
  • Second are Strings of Text; you can use the strings of text when needed. Example: “First Attempt”, etc.
  • Finally, Boolean. Example: True value and False value. 

In addition to the above-mentioned data types, trivial data types are also supported by JavaScript, the null trivial data type, and the undefined trivial data type. These data types can be used to define only single values. Objects, also known as composite data types, are also supported by JavaScript. Explore More about Objects; requires a basic understanding of JavaScript to begin with. 

It is important to know that the numbers provided as input in JavaScript are considered to be floating-point values. There is no segregation between the integer values and the numerical values represented in the form of decimals (floating point values). The IEEE 754 standard, practicing floating point format (64-bit) is implemented in JavaScript.

With some basic understanding of JavaScript data types, let us now explore JavaScript variables.

A certain carrier or a container is required to represent the data type in JavaScript programming. These containers are known as variables. With the help of variables, the input data can be represented uniformly across the program by simply mentioning the name of the variable. 

But how does a JavaScript program identify a variable? When we use a variable to represent a value of a certain data type, the first step is to declare the variable. A JavaScript program uses the keyword “var” to declare a variable. 

Following is an example of a JavaScript code for declaring a single variable:

<script type = "text/javascript">
   <!--
      var attempts;
      var counts;
   //-->
</script>

Following is an example of a JavaScript code for declaring multiple variables:

<script type = "text/javascript">
   <!--
      var attempts, counts;
   //-->
</script>

The variable used to represent a certain value also requires it to store the value first to represent it uniformly across the program; the storing of the data is achieved by the variable initialization process. The process can be carried out at the time of variable declaration or at a later point in time. 

When the two variables' attempts and counts are created, values can be assigned to these two variables right after declaring them or later on when the variables are initialized. 

For example:

<script type = "text/javascript">
   <!--
      var attempts= "A";
      var counts;
      counts= 5;
   //-->
</script>

A variable should only be declared once in a program file; any redeclaration of the variables will cause errors. 

Javascript language eliminates the need to specify the data type of the value being entered while declaring and initializing a variable. Unlike other programming languages, JavaScript can store any type of data in a variable. This is why JavaScript is also known as an untyped language because there is no need to mention the variable's data type. Automatic identification of the data type of the variable can be performed by JavaScript when the program is executed. 

The program needs to understand the scope of the variable being declared. This is important because it helps the compiler access the variables to a particular section or a block of code when needed (depending on the definable region of the variable). Mainly, there are two types of variable scopes. 

The first type of variable scope that a variable can have is a global scope, meaning these variables are declared globally and can be accessed throughout the JavaScript program. These variables having global scope are known as global variables. 

The second type of variable scope that a variable can have is a local scope, meaning these variables can only be used within a function in which they are defined. These variables having local scope are known as local variables. The locality of the parameters of a particular function is to itself. 

When the local variable is declared with the name of a global variable within a function, in this case, the local variable hides the globally declared variable having the same name, and the function only accepts the locally declared variable. This helps the function to identify the locally declared variables, reject the global variable, and prevent the program from any unwanted errors. 

Following is an example of a JavaScript code for the above-mentioned condition:

<html>
   <body onload = checkscope();>   
      <script type = "text/javascript">
         <!--
            var Varone= "attempt";      // Declaring a global variable
            function checkscope( ) {
               var Varone= "count";    // Declaring a local variable
               document.write(Varone);
            }
         //-->
      </script>     
   </body>
</html>

The output for the program is: 

count

Also, there are many other situations where how you name the variable can cause unwanted probes for the JavaScript program. Following are some important points that you can follow to avoid any unnecessary situations. 

This is a no-brainer, when naming a variable, you have to avoid the use of any keywords strictly. For example, keywords such as export, throw, this, etc., are non-valid names for a variable. 

When naming a variable, the name should not start with a numerical value meaning the variable should always start from a character or an underscore. Example: 45tokensnumbers or 56counts are non-valid names for a variable. 

Also, pay attention to the variables' fonts as the variables' names are case sensitive in JavaScript, so when declaring attempts and Attempts, JavaScript will consider these two variables as different variables. 

A list of words that are already reserved in JavaScript and cannot be used to name any variables, loop labels, methods, etc. 

  • Words starting with A: abstract 
  • Words starting with B: boolean, break, byte
  • Words starting with C: case, catch, char, class, const, continue 
  • Words starting with D: debugger, default, delete, do, double, 
  • Words starting with E: else, enum, export, extends
  • Words starting with F: false, final, finally, float, for, function, 
  • Words starting with G: goto
  • Words starting with I: if, implements, import, in, instanceof, int, interface
  • Words starting with N: native, new, null
  • Words starting with P: package, private, protected, public
  • Words starting with R: return
  • Words starting with S: short, static, super, switch, synchronized
  • Words starting with T: this, throw, throws, transient, true, try, typeof
  • Words starting with V: var, void, volatile
  • Words starting with W: while, with