Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Variables

JS Variables

JavaScript variables are used to store data and values.

JavaScript Identifiers

All JavaScript variables must be identified with unique names. These unique names are called identifiers.

Rules for defining names for variables (unique identifiers) are:

  • Names can contain letters, digits, underscores, and dollar signs.
  • Names must begin with either a letter, an underscores or a dollar signs.
  • Names must not start with numbers
  • Names are case sensitive
  • Reserved words (like JavaScript keywords) cannot be used as names.
  • Though we can use alphabets (like x, y, Z, etc.) as variable names but is suggested that use a descriptive name for the variable (like name, price, address, etc.).
Variable Declaration

Creating a variable in JavaScript is called "declaring" a variable.

4 Ways to Declare a JavaScript Variable:

  • Using var
  • Using let
  • Using const
  • Using nothing
  • It is recommended that use either var, let, or const to declare a variable
					 
        
          var a;
          let b;
          const c = a + b;

          // Multiple declaration in single line
          var x, y, z;
        
      
Variable Assignment

After the declaration, the variable has no value (technically it is undefined)

					 
        
          a = 5;
          b = 3;
          c = a + b;

          // Multiple assignment in single line
          x = 3, y = 2, z = x + y;
        
      
Multi Line Declaration & Assignment
					 
        
          var a = 5,
          b = 5,
          c = a + b;
        
      
Comparison of var, let and const Variables

All var, let and const are used to declare variables but all serve different purpose.

# var let const
Assignment Not necessary Not necessary Must assign value when declare
Usability Can be used before declaration Must declare before use Must declare before use
Re-declare Yes No No
Re-assign Yes Yes No
Scope Global Block Block
When To Use Use this if you want a variable to access anywhere in the program (globally) Use this if you do not want a variable to re-declare even outside the block scope Use this when you know that the value must be fixed like (arrays, objects, functions, regex, etc)
Declaration & Assignment
					 
        
          var a;   // valid
          a = 5;   // valid

          let b;   // valid
          b = 5;   // valid

          const c;   // invalid
          c = 10;   // invalid

          const c = 10;   // valid
        
      
Re-declaration
					 
        
          var a;   // valid
          var a = 5;   // valid

          let b;   // valid
          let b = 5;   // invalid

          const c = 5;   // valid
          const c = 5;   // invalid
        
      
Re-assign
					 
        
          var a = 5;   // valid
          a = 10;   // valid

          let b = 10;   // valid
          b = 15;   // valid

          const c = 20;   // valid
          c = 30;   // invalid
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved