Creating variables in JavaScript is called "declaring" variables, then we can declare JavaScript variables through the var keyword. The declaration syntax is "var carName;" and the syntax for assigning values to variables is such as "carName. = "porsche";".
The operating environment of this article: windows7 system, javascript version 1.8.5, DELL G3 computer
javascript definition variable writing method
Declare (Create) JavaScript Variables
Creating a variable in JavaScript is called "declaring" a variable.
You can declare JavaScript variables through the var keyword:
var carName;
After declaration, the variable has no value. (Technically, its value is undefined.)
To assign a value to a variable, use the equal sign:
carName = "porsche";
You can assign a value to a variable when you declare it:
var carName = "porsche";
In the above example, we created a variable named carName and assigned the value "porsche" to it.
Then, we "output" the value in the HTML paragraph with id="demo":
Example
<script> var carName = "porsche"; document.getElementById("demo").innerHTML = carName; </script>
Recommended study: "javascript Advanced Tutorial》
The above is the detailed content of How to define variables in javascript. For more information, please follow other related articles on the PHP Chinese website!