JavaScript Variable

Variables represent the memory storage areas used in a program to hold values needed in a program. A variable is a name given to a location in computer memory for values used in a program . A variable can be initialized at the time of declaration of a variable. As the name suggests, the value of a variable can be updated in the program by assigning a new value or give a formula on the left of the assignment statement. JavaScript variable is used in a webpage script to utilize programming flexibility in a website.

JavaScript is a loosely-typed scripting language. It means that a variable can store different data type values at different locations in the script.

Features of a JavaScript Variable

  • A JavaScript variable can store Boolean, Number or String values or Objects
  • Variables can be initialized with a value. If a variable doesn’t contain any value it remains undefined. If a variable becomes empty it holds a null value.
  • JavaScript variables can also be declared using keyword var. This is most commonly used when you need to declare same named global variables and local variables.
  • Multiple variables can be declared in one statement by using var keyword and separating all of them with commas.
  • Variable names can start only with an alphabet or underscore (_). The name should not have spaces but can have alphabets, digits and underscore.
  • Variable names are case-sensitive and should not be a keyword.
  • One variable can hold values of different data types within a script at different points of execution. For example variable age is assigned number value. After a few statements age variable can be assigned a string value.
  • You don’t need to specify the data type of a JavaScript Variable. The value assigned to a variable will automatically define its data type.

Keywords

Keywords the special terms shipped with a programming language. They have an inbuilt meaning and can be used any number of times in a script or a program. A keyword cannot be used as a name for a JavaScript variable. Read about the full set of JavaScript Keywords.

Data Types for a JavaScript Variable

JavaScript variables can hold the following type of data-

Integer

Integers are the numbers without any fractional or decimal part. A JavaScript Integer can be stored as Octal(base 8), Decimal(base 10) or Hexadecimal (Base 16) numbers

Float

Float or floating point numbers are used to express the numeric value in decimal form or exponent form (mantissa and exponent).

Boolean

Boolean variables represent true/false, yes/no or 1/0 values. The allowed values for a Boolean JavaScript Variable are true or false

String

The text values in JavaScript are represented by a sequence of characters in single or double quotation symbols. Special characters called Escape Sequence can also be used in string to give a different meaning to a character. These are predefined and you cannot create them on your own. Each escape Sequence starts with a backslash character (\) followed by another character. For example ‘\n’ is used for new line and ‘\t’ to add horizontal tab.

Null

Null is anything which is empty or non-existent. When a JavaScript variable becomes empty due to some nonexistent value it is  said to hold a Null value.

Example JavaScript Variable

<BODY>
<SCRIPT LANGUAGE="JavaScript">
	name="CSVeda.com";
	age=5;
	document.write("<H3>Name="+name+":</H3> Age:	"+age+" Years old!!!");
	age="Hello";
	name=123456
	document.write("<H3>Name="+name+":</H3> Age: "+age+" Years old!!!");
</SCRIPT>
</BODY>
JavaScript Variable example