[Javascript study notes] JS variables can be used without declaration, directly invincible in the world!

What is a variable

Essence: A variable is a piece of space that the program applies for in the memory to store data.

variable

Variable input and output

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        // var age;
        // age=18;
        // console.log(age);
        // var myname='41';
        // console.log(myname);
        // 1.输入用户姓名
        var myname=prompt('input your name');
        // 2.输出这个用户名
        alert(myname);
    </script>
</head>
<body>
    
</body>
</html>

Declare multiple variables at the same time

When declaring multiple variables at the same time, you only need to write one var, and use commas to separate multiple variable names

var age=10,name='41',sex=2;

Declare variables special cases

JS can be used directly without declaration, invincible in the world

Happening Description result
var age; console.log (age); Only declare, not assign undefined
console.log(age); Do not declare, do not assign, use directly Report an error
age=10;console.log(age) No declaration but assignment 10

Variable naming convention

  • There are letters, numbers, and underscores. Dollar sign composition
  • Strictly case-sensitive, var app; and var APP; are two variables
  • Cannot start with a number
  • Cannot be keywords, reserved words
  • The variable name must be meaningful
  • Follow the camel case nomenclature, lowercase the first letter, and capitalize the first letter of the following words, such as myFirstName
  • Recommended translation website: Youdao iCIBA

Guess you like

Origin blog.csdn.net/qq_42136832/article/details/115164997