How to define variables in javascript

藏色散人
Release: 2023-01-07 11:42:54
Original
2129 people have browsed it

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;
Copy after login

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";
Copy after login

You can assign a value to a variable when you declare it:

var carName = "porsche";
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template