Variables in JavaScript

Variables in JavaScript

Variables in javascript: A javascript  is simply a name of storage location, variables are case sensitive, there are three types of variables in javascript

  • Var
  • Let
  • Const

Var: a Variables is  an identifier or a name which a user to refer a value

Syntax:

Var  <Variable-name>;
Var <Variable-name>=value;

Example:

<!DOCTYPE html>
<html>
     <body>
          <script>
              var FirstName="Programming master";
              document.write(FirstName);
          </script>
    </body>
</html>

Output:

Programming master
Example:

<!DOCTYPE html>
<html>
     <body> 
         <script> 
               var x="Programming master"; 
               document.write(x); 
               document.write(x); 
               document.write(x); 
         </script> 
     </body> 
</html>

Output:

Programming master

Programming master

Programming master

Example:

Override variable value

<!DOCTYPE html>
<html>
     <body>
            <script>
                   var FirstName="Programming master";
                   FirstName="JavaScript";
                   document.write(FirstName);
            </script>
      </body>
</html>

Or

<!DOCTYPE html>
<html>
      <body>
            <script>
                var FirstName="Programming master";
                var FirstName="JavaScript";
                document.write(FirstName);
            </script>
      </body>
</html>

Output: JavaScript

  • Let : in let once we declare a variable name, we cannot declare same variable name again and again otherwise it will give an error

Example :

<!DOCTYPE html>
<html>
     <body>
          <script>
              let FirstName="Programming master";
              document.write(FirstName);
          </script>
    </body>
</html>

Output:
Programming master
Example:

<!DOCTYPE html>
<html>
    <body>
         <script>
              let FirstName="Programming master";
              FirstName="JavaScript";
              document.write(FirstName);
         </script>
    </body>
</html>

Output : JavaScript

Example:

Error : Identifier ‘FirstName’ has already been declared

<!DOCTYPE html>

<html>
       <body>
             <script>
                   let FirstName="Programming master";
                   let FirstName="JavaScript";
                   document.write(FirstName);
             </script>
       </body>
</html>
  • Const: once you declare a variable name and you assign a value to that variable name you cannot reassign name and value

Example:

<!DOCTYPE html>
<html>
      <body>
             <script>
                    const FirstName="Programming master";
                    document.write(FirstName);
             </script>
     </body>
</html>

Output:

Programming master
Example: Error Assignment to constant variable

<!DOCTYPE html>
<html>
       <body>
          <script>
              const FirstName="Programming master";
              const FirstName="JavaScript";
              document.write(FirstName);
         </script>
    </body>
</html>

Example :Error  Identifier FirstName has already been declared

<!DOCTYPE html>
<html>
      <body>
           <script>
               const FirstName="Programming master";
               const FirstName="JavaScript";
               document.write(FirstName);
          </script>
     </body>
</html>