Home  >  Article  >  Web Front-end  >  How to define variables in javascript

How to define variables in javascript

藏色散人
藏色散人Original
2021-07-12 11:15:182083browse

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";".

How to define variables in javascript

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 = &quot;porsche&quot;; 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn