Loading, please wait...

Variables

JavaScript Variables

 Variables are containers for storing data values. A variable can have different values at different phases of program execution.

 

Here, we use the var keyword to declare a variable. The variable name must be unique and further the value to the variable assigned by using the equal to (=) operator.

 

For example,  

var x = 20; // Here variable x stores the value 20

 

JavaScript Variable names are case- sensitive

For example, var x and var X are two different naming variables.

 

There are different naming rules are as follows:

 

  • Here, the first character must contain a letter, an underscore (_), or a dollar sign ($).
  • Names must begin with a letter.
  • Numbers are not allowed as the first letter but further names can contain numbers.
  • JavaScript Names must not contain spaces.

 

 

Assignment Operator (=)

In JavaScript, the equal sign (=) is called the “assignment” operator, rather than an “equal to” operator.

Assignment operator mainly used to assign values to the variables.

 

For example, 

x = 5 will assign the value 5 to x.

 

 

Code:

<html> 
<head> 
</head> 
<body> 
<script>
  var x = 100;
  document.write(x);
</script>
</body> 
</html>

 

Here, the variable x assigns a value of 100 with the help of an assignment operator.

 

 

 

Output: