Home  >  Article  >  Web Front-end  >  How to use JavaScript variables

How to use JavaScript variables

不言
不言Original
2018-12-12 17:44:082634browse

When writing a program, we need to use various data in each place. Sometimes we also need to reuse strings and the same values ​​​​and calculation formulas. If we write them all, it will be very troublesome. At this time, the variables It will be very useful.

How to use JavaScript variables

First let’s take a look at what JavaScript variables are?

JavaScript variables can be used to store values ​​(such as x=2) and expressions (such as z=x y). Variables can have short names (such as x and y) or more descriptive names.

Variables must start with a letter

Variables can also start with $ and _ symbols (but we do not recommend this)

Variable names are case-sensitive (y and Y are different variables)

Then let’s take a look at how to use JavaScript variables

The writing method of variables in JavaScript is as follows

var How to use JavaScript variables名 = “值、字符串”;

When using var to complete the variable declaration, replace the value and string data attached to the right side "" with the variable on the left side, and use the assignment operator "=".

Therefore, if you want to put the string string in a variable named name, you need to perform the following operations

var name = "string"

In addition to var, variables also include let and const. About these three You can refer to the articleThe difference between let, var and const in JavaScript and the introduction of application scenarios, so I won’t go into details here.

Let's use the var variable to see a specific example

In this example, we define "string" as a variable and use document.write method calls it. By using the command document.write(), numerical values ​​and strings can be displayed.

The code is as follows



  
    
    JavaScript
  
  
    <script>
      var name = &quot;string&quot;;
      document.write(name);
    </script>
  

The running effect on the browser is as follows

How to use JavaScript variables

Finally, we can also implement pop-up boxes Notification

Pop-up notification can be easily implemented in JavaScript, which is helpful when confirming the execution results.

alert(How to use JavaScript variables);

By the way, this time put the calculation formula into the variable and output it. In the case of numbers, since they are not strings, "" is not used.

The code is as follows

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript</title>
  </head>
  <body>
    <script>
      var i = 1 + 100;
      alert(i);
    </script>
  </body>
</html>

The operation effect is as follows

How to use JavaScript variables

Assign the value "1 100" to variable i and output the total number .

The above is the detailed content of How to use JavaScript variables. 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