javaScript Variables

you use variables to store, retrieve,and manipulate values that appear in you code.

javascript variables are 'containers' for storing data:


a javascript variable is a named location in memory that is used to hold a value that can be modified by the program.

declaring a variable is the process of telling the interpreter to get the ready to store data in a new javascrip variables are declared using the var keyword with the name of the variable you wish to declare.

javascript variable naming rules

the rules for creating legal variable names are as follows:

  • the first character of a variables name cannot be a digit.
  • javascript is case-sensitive language.this means mycount is differ from the mycount.
  • the variable name must not be a reserved word.
  • A variable name cannot start with a number (0-9)
  • subsequent characters must be letters, numbers, dolar $, or underscores(_).


        legal js variable names                        |          illegal js  variable   names

                          javascript                                  javas cript   
                          java888                                     888java    
                          _this                                         this
                          $_java                                       case
                          va_lu_e                                     -va-lu_e

javaScript - case sensitivity

javascript is case-sensitive language.this means that capital letters are distinct from their lowercase counterparts.for example, if you use the identifiers case, case, case in your script, each identifier refers to a separate, distinct variable.

case sensitivity applies to all aspects of the language: keywords, operators, variable names, event handlers, object properties and so on.all javascript keywords are lowercase.

declaring javaScript Variables

a variable stores data.every variable has a name, called its identifier.

you declare javascript variables with the var keyword:

              var    name;

note:a variable declared using the var statement with no initial value specified has the value undefiend.

the undefined is a data type in javascript

to assign a value to the variable, use the equal sign:

                name="javascript variable declaration";
                age = 34
                bool = true

example - how declare a variable in javascript

              <p   id="int"></p>

              <p   id="name"></p>

               var     value = 20000;
               var     name  = "javascript variable declaration";
               document.getelementbyid("int").innerhtml= value ;
               document.getelementbyid("name").innerhtml= name ;


note :- you can declare many variables in one statement.start the statement with var and separate the variables by comma:

     var   name = "javascript  variables" ,  last_name = "variables",  old = 25, boll = true;

implicit variable declaration

when you use an undeclared variable on the left-hand side of an assignment, the variable is automatically declared in javascript.

note : you should not use this variant.

             widgates = 10 ;


      var   widgates = 10 ;

javaScript constant variables

you can declare constant variables with the const keyword.

you cannot declare a constant with the same name as a function or variable in the same scope.

syntax: how declare constant variables in javascript

         const    name = "javascript", age = 20;
           const   name  = "constant variables";
           const   value = 20000 ;
           const   bool  = true ;
           const   data  = 23232323.232323;

JavaScript Variable Scope

the scope of a variable is all parts of a program where it is visible.visible means that the variable has been declared and is available for use.

a variable that is visible everywhere in the program has global scope.a variable that is visible only in a specific context like a function has local scope.

all of variables that declared inside the function have local scope.

javaScript - weakly typed language

the high-level languages including c, c++, java are strongly typed.that is, a variable must be declared before it is used, and its type must be included in its declaration.once a variable declared, its type cannot be change.

weakly typed language variable and literal has a type, but data types are not explicitly declared.for example, we might define a variable datavalue to hold number and set it to a value of 4444.

          var   datavalue ;  // undefined datatype
                 datavalue = 4444;   // now number  datatype
                 datavalue = "java"  // now string  datatype

definition :- in the above example first we create a variable datavalue at this point its datatype is actually the type undefined. then we assign it to the number 4444, so its datatype is number (4444).next we reassign it to the string "java", so the variable type is now string.its means javascript is a weakly typed language.