Home >Web Front-end >JS Tutorial >Selected variable basics of JavaScript (super detailed learning sharing!)
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 变量名 = 变量值Example:
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;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
<!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>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
\',
\",
\\ ,
\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 = falseBoolean 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
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>[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!