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.
var x = 20; // Here variable x stores the value 20
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.
Assignment Operator (=)
Assignment operator mainly used to assign values to the variables.
x = 5 will assign the value 5 to x.
<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.