Javascript Variables

  • Variable is like an empty box that can be used to store information. You can place anything inside the box.
  • It’s a temporary place to store values.
  • Variables are pieces of memory allocated to store information.
  • Variables are defined using keyword var in JavaScript. You can assign any name to variable, just like you can place any name on your empty box however there are some rules for naming variables.
  • Variable values can be used by calling the name of variable.

JavaScript Variable Syntax

var NAME_of_Array = VALUE;

JavaScript Variable Example

  • In above example var is used to create a variable.
  • myname is the name we assigned to our variable
  • Shahid is the value we saved in our variable
  • on this HTML page we can used document.write followed by the name of variable and it will show the value of variable.

Rules for Naming Variable

  • Variable names are case sensitive
  • Variable names cannot start with numbers
  • Variable names cannot contain spaces
  • Variable names can contain letters, numbers, $, _