In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces what are the variables and data types of JavaScript. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Foreword:
I'm not in the front end, but in the back end. The native programming language is java. I studied js because I saw that my roommate could make a dynamic web page, but I could only make a static web page, plus I had to learn it next semester, so I came to study in advance.
Warm reminder:
Java and javsScript do not have a dime relationship, but after javaScript was acquired by SUN, it was changed to jaaScript, first because SUN's main product is java, and the second is to use the popularity of java to advertise javaScript to expand javaScript's influence.
Next, let's share today's practical information.
Variable
What is a variable?
We need to use programming languages to deal with all kinds of data in real life, and where is all kinds of data stored? The answer is variable, variable is not a noble thing, it is a box of things, it is not too much to call it a plastic bag. The essence of a variable is to open up a space in memory to store data. Similar to our hotel room, a room can be seen as a variable.
The use of variables
Variable use is divided into two steps, 1. Declare variables, 2. Assign a value. We still use the same idea to understand these two steps. One day I came to a hotel and I told my boss to open a single room. When I paid for it, the boss gave me a room card, which meant that I could stay in that room within a certain period of time. (I pay, the boss gives the card, which is equivalent to a statement.) after I checked in, the vacant room was occupied, which was equivalent to the assignment.
Let's take a look at the use of variables in JS
1. Declare var age
Var is a keyword of js, which is used to declare variables (meaning of variable variables). After using this keyword to declare variables, the computer automatically allocates memory space for variables
The variable name defined by the age programmer, through which we access the space allocated in memory.
two。 Assign var age=19; / / assign the variable age to 19
= is used to give the value on the right to the variable space on the left, which is the assignment
Variable values are values that programmers save to spatial variables
3. Two small details of grammar
Update variable: after a variable is reassigned, its original value is overwritten, and the value of the variable is subject to the last assigned value.
The final result of var age=18;age=19;// is 19. 5. Because 18 is covered by 19.
Declare multiple variables at the same time: just write a var, and then separate the variable names with a comma
Var age,number,average
Special cases of declaring variables
Special one
Var sex; only declares, does not assign a value, and the program does not know what it is, so the result is undefined (undefined) console.log (sex)
Special two
Console.log (sex); do not assign, do not declare, directly use a variable will report an error
Special three
Qq=90;console.log (qq); do not declare the direct assignment directly use, will not report an error! This is outrageous, but it is true in javaScript because it is too free. Naming conventions for variables
Consists of letters (AZ,az), underscore _, and dollar sign $, such as userName
Strictly case-sensitive. Var app; and var App; are two variables
Cannot start with a number, such as 12age is wrong
Can not be keywords, reserved words, such as: var, for, while.
The variable name must have meaning, so that you can see the name and know the meaning.
Follow the hump nomenclature: the first letter is capitalized, and the first letter after the word needs to be capitalized. Such as: myName
Recommended translation website: Youdao, or direct Baidu
Why do you need data types?
Programming language is used to deal with real-life problems, we have to deal with a variety of data in the real world, including integers, decimals, text and so on, corresponding to integers, floating-point numbers, characters in the programming language. In the computer, different types of data occupy different storage space, in order to facilitate the data to be divided into different memory sizes of data, make full use of storage space, so different data types are defined. To put it simply, the data type is the category model of the data, that is, the classification of the data. Such as name, "Ye Qiuhan", age 18, the type of these data is not the same.
The data type of the variable
Variables are the places where values are stored. They all have their names and types. The data type of the variable determines how the bits representing these values are stored in the computer's memory. JavaScript is a weakly typed or dynamic language, which means that there is no need to declare variables in advance.
Type, the program will automatically be executed.
Var age=10; / / this is a numeric var name=' Ye Qiuhan'; / / this is a string
During the running of the code, the data type of the variable is determined by the js engine according to the data type of the variable value on the right side of =. After running, the variable determines the data type.
Dynamic js has dynamic types, which also means that the same variable can be of different types.
Var xtriple 6; / / x is the number var Xuan 'bilibili'; / / x is the string simple data type (basic data type)
Simple data types and their descriptions in js
Simple data type description default value Number numeric type, including integer value and floating point value, for example, 12Power0.20220 Boolean Boolean value is a type, such as true,false, which is equivalent to 1 and 0falseString string type, such as "Ye Qiuhan", note that the string is in quotation marks "" Undefinedvar an in js; declare variable a but does not give a value, then a=undefinedundefinedNullvar a null; declare a variable as null null numeric type
1. Digital base system
The most common bases are binary, octal, decimal, and hexadecimal.
/ / 1. The octal number sequence range 0~7var num1=07; / / corresponds to the decimal 7var num2=019; / / corresponds to the decimal 19var num3=08 / / corresponds to the decimal 8 prime 2. Hexadecimal digit sequence range: 09and A~Fvar num=0xA
Now we just have to remember to add 0 before octal and 0x before hexadecimal in js
two。 Digital range
Maximum and minimum values of values in js
Alert (Number.MAX_SAFE_INTEGER); / 9007199254740991 alert (Number.MIN_VALUE); / / 5e-324
3. Three special values of digital type
Alert (Infinity); / / Infinity alert (- Infinity); / /-Infinity alert (NaN); / / NaN
Infinity, which stands for infinity, greater than any value
-infinity, which stands for infinitesimal, less than any value
NaN, Not a number, represents a non-numeric value
String String
The string type can be any text in quotation marks, and its syntax is single quotation mark * * 'and double quotation mark "" * *
Var srtAge ='18'; var strName ='Ye Qiuhan'; var srtFood ='I love junk food'; / / Common error var srtNum2 = 11; / / error, without quotation marks, is considered js code, but js does not have these syntax
Because the attributes in the HTML tag use double quotation marks, we recommend using single quotation marks here in js
String quotation marks nesting
Js can use single quotation marks to nest double quotes, or double quotes to nest single quotation marks (outer double inner single, inner single outer double).
Var strMsy ='I am 'programmer Xiao Han'; console.log (strMsy) var strMsy2 ='I am 'programmer Xiao Han'; console.log (strMsy2); / / Common error var badQuotes = "What on earth?" ; console.log (strMsy2)
The results are as follows
String escape character
Similar to the special characters in HTML, there are also special characters in the string, which we call escape characters.
Escape characters all start with\. The commonly used escape characters and their descriptions are as follows
Escape characters explain\ nnewline character, n means newline\\ slash\\ 'single quotation marks\ * double quotes\ ttab indentation\ b space, b means blank
Boolean Boolean
Boolean types have two values, true and false, where true stands for true and false for false
When Boolean and numeric are added, the value of true is 1, and the value of false is 0.
Console.log (true+1); / / 2 console.log (false+0); / / 0
Undefined and Null
A variable that is not assigned after declaration will default to a default value of undefined (if added or concatenated, pay attention to the result)
Var variable; console.log (variable); console.log ('Hello' + variable); console.log (11+variable); / / + acts as a connection console.log (true+variable)
The results are as follows
A variable is given a null value, and the value stored in it is empty, but there is nothing.
Var vari=null; console.log ('Hello' + vari); console.log (11+vari); console.log (true+vari)
The results are as follows
What is the conversion of data types
Using the form, the default value of the data obtained by prompt is a string type, so you can't do a simple addition operation at this time, but you need to convert the data type of the variable, in short, to convert one data type to another.
Three commonly used conversion methods
Convert to character type
Convert to digital
Convert to Boolean type
1. The conversion to string mode illustrates that the case toString () is converted to the string var num=1; alert (num.toString ()); the String () cast is converted to the string var num=1; alert (String (num)); the result of the plus sign concatenation of the string and the string concatenation is the string var num=1; alert (num+ "I am a string")
ToString () and String () use play in the same way
Three kinds of conversion methods, the first two kinds of understanding, the third kind of conversion is commonly used, and also becomes invisible transformation.
two。 Convert to digital (key)
(1) use the functions provided by js
Js provides two conversion functions, parseInt () and parseFloat (). The former converts a value to an integer, while the latter converts a value to a floating point number.
Just look at the code and understand.
Var age=12; console.log (age); / / use parseInt () to convert values to integers console.log (parseInt (age)); console.log (parseInt ('3.12')); / / round console.log (parseInt (' 3.89')); / / round console.log (parseInt ('10px')) Console.log (parseInt ('rem120px')) / / NaN / / converts the value to an integer console.log (parseFloat (' 3.14')) using parseFloat (); / / 3.14 console.log (parseFloat ('120px')); / / 120 removes the px unit console.log (parseFloat (' rem102px')) / / when the NaN js engine reads rem, it does not recognize it and directly determines that it is empty.
(2) cast Number () conversion function
Var str='123'; console.log (Number (str)); / / 123 console.log (Number ('12')); / / 12
(3) the invisible conversion of js (- * /) Note that there is no +, and + plays a splicing role.
Console.log ('122color color 0); / 12 console.log (' 122piano 1); / / 1221 console.log ('123piano Boolean); / / 15129 console.log (' 123minute Boolean); / / 1 conversion to Boolean mode indicates that the other types of case Boolean () function are converted to Boolean Boolean ('Boolean).
For null, negative values will be converted to false, such as'', 0 ~ ~ Na ~ Null ~ ~ undefined
The remaining values are converted to true
Console.log (''); / / false console.log (0); / / false console.log (NaN); / / false console.log (null); / / false console.log (undefined); / / false console.log ('Xiaobai'); / / true console.log (12) / / true about JavaScript variables and data types are shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.