Tuesday, May 9, 2017

Variables

Variables can be thought of as named containers or A placeholder for a value. Data can be placed into these containers and then refer to the data simply by naming the container.
JavaScript is untyped language. So, JavaScript variable can hold a value of any data type. Therefore , during variable declaration you don't have to tell JavaScript the type of value that variable will hold.

Note that,

  • Variable must be start with a upper case letter, lower case letter or underscore. As an example we can declare variable as follows;
    • name
    • Name
    • _name
  • Number is not allowed to use as the first character of variable
  • Can't use spaces in variables
  • Not allowed to use JavaScript keywords such as var, function etc
  • Variable name is case sensitive

Variables are declared with the var keyword as follows.
var name ;

Then variable can be initialized as,
name = "Sanduni";

We can declare variable and assign value in same line by
var name = "Sanduni";

As an example, 

This is the result we got

Hope you'll understand variables. In upcoming tutorial let's learn about JavaScript variable scope which means the region of the program where the variable is defined.

No comments:

Post a Comment