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!
##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 变量名 = 变量值
var name ; //声明一个变量 var name,city,like ; //声明多个变量,多个变量之间用英文状态下的逗号分开 var name = "阿泽"; //声明一个变量并且赋值
1.3 Naming of variables Rules
#2. Data type of variable
The key to the type of variable lies in the type of value2.1 Numeric type
Numeric type variables can be used for mathematical operations, including: integer type, floating point type and NaNvar a = 10; var y = 0.1; var x = 100;
<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>
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
<!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>
backslash (\) . Commonly used escape characters for
\',
\",
\\ ,
\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
var a = true; var b = false
<!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
undefined.
<!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
null.
<!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>
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!