Home >Web Front-end >JS Tutorial >Selected variable basics of JavaScript (super detailed learning sharing!)

Selected variable basics of JavaScript (super detailed learning sharing!)

WBOY
WBOYOriginal
2021-10-12 17:24:481979browse

This article talks about the basics of JavaScript variables. First, let’s talk about the basic concepts of variables and the data types of scalars. I hope it will be helpful to everyone, let’s work together!

Selected variable basics of JavaScript (super detailed learning sharing!)

  1. Variables

##1.1 The concept of variables:

A variable refers to a named storage unit in the program. Its main function is to provide a container for storing information for data operations. A variable can be thought of as a container that holds data.

1.2 Declaration and assignment of variables

In JavaScript, variables need to be declared before using them. The system keyword var is used to declare variables. When declaring a variable, you can also use the assignment number "

=" to assign a value to the variable. The syntax format is as follows:

var 变量名 = 变量值

Example:

var  name ;  //声明一个变量
var name,city,like ; //声明多个变量,多个变量之间用英文状态下的逗号分开
var name = "阿泽"; //声明一个变量并且赋值

1.3 Naming of variables Rules

  • Variables cannot start with a number and can start with a letter or an underscore.


  • #JavaScript variable names are case-sensitive.

  • You cannot use keywords in JavaScript as variable names.

#2. Data type of variable

The key to the type of variable lies in the type of value

2.1 Numeric type

Numeric type variables can be used for mathematical operations, including: integer type, floating point type and NaN


Example:

var a = 10;
var y = 0.1;
var x = 100;

The more special one is: NaN (not a number) is not a number. When converting other data types into numeric types, the conversion cannot be performed, but the program cannot report an error. In this case, a NaN value will be returned. The following situation:

<html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
           var length = "500m";
           length = length*5;
           document.write(length);
        </script>
    </head>
    <body>
    </body>
</html>

Now we want the length of something to be 5 times its original length. A string cannot be converted into a meaningful value and can only be converted into NaN

A string of numbers can be converted into a meaningful numerical value. The length can be modified to a string of pure numbers and the viewing result can be output.

2.2 Character type

A string enclosed in single quotes or double quotes.


It should be noted that single quotes and double quotes can be nested within each other; only double quotes can be nested within single quotes, and only single quotes can be nested within double quotes.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
    var name = "阿泽";
            var str = "我的名字是&#39;" +name+"&#39;"
            document.write(str)
    </script>
   </head>
<body>
    </body>
</html>

The plus sign is the string connector. If you want to nest double quotes within double quotes, the double quotes inside must be escaped (\"). Escape characters in JS It is

backslash (\) . Commonly used escape characters for

are:

\', \", \\ , \r, \n, etc.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
    var name = "阿泽";
            var str =  "我的名字叫做\"" +name+"\""
            document.write(str)
    </script>
   </head>
<body>
    </body>
</html

2.3 Boolean type

Boolean type is also called logical type. There are only two values: true (true), false (false). That is, two states, such as: light switch, gender, etc.

var a = true;
var b = false

Boolean types are commonly used in if conditional judgment statements, for example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
     var a = 10;
           var b = 110;
           if(x>y){
               document.write(a+"比"+b+"大");
           }else{
               document.write(b+"比"+a+"大");
           }
    </script>
   </head>
<body>
    </body>
</html>

2.4 Undefined type

The value of the undefined type is only A

undefined.

When a variable is defined and is not assigned a value, it will be returned as undefined; when an object's attributes do not exist, it will also be returned as undefined;

Example: Undefined Assignment


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
      var x;
           document.write(x);
    </script>
   </head>
<body>
    </body>
</html>

2.5 Empty type

The empty type has only one value:

null.

When an object does not exist, a null type will be returned; if you want to clear the value of a variable, you can assign a null value.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
      var x = null;
     var a = 100;
     var a = null ;
     document.write(x);
     document.write(a);
    </script>
   </head>
<body>
    </body>
</html>

[Recommended learning:

javascript advanced tutorial]

The above is the detailed content of Selected variable basics of JavaScript (super detailed learning sharing!). 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