JS Preliminary Study

Source: Internet
Author: User

[Three ways to use JS] 1, HTML tags Embedded js (not advocated): <button onclick= "Javascript:alert (' Little Green Pond! You're a bit of a man! ' > have the ability to point me!!! &LT;/BUTTON&GT;&NBSP;2, HTML page directly using Js:<script type= "Text/javascript" &GT;//JS code </script> 3, referencing external JS file : <script language= "javascript" src= "JS file path" ></script> [considerations]① Page JS Code and Reference JS code, can be embedded in the HTML page anywhere. However, the location will affect the execution sequence of the JS code, for example,:<script> in front of the body, will be executed before the page loading JS code,  ② page JS code, using type= "Text/javascript" reference to the external JS file, Use the language= "JavaScript"  ③ to reference the external JS file <script></script> tags, must appear in pairs, and the label can not have any code inside!    "JS variables" 1, JS in the wording of the variable declaration: var width = 10; Variables declared with Var are valid only at the current function scope width1 = 11; Do not use Var, the direct assignment generated by the variable, the default is the global variable, the entire JS file effective var a,b,c=1; The same declaration statement declares multiple variables at the same time, separating the variables with commas. However, the assignment needs to be assigned individually, such as in the previous formula, where only C is assigned as 1,a/b to undefined (undefined)  [declaration variable considerations]①JS all variable type declarations in the VAR keyword. The specific data type of the variable, depending on the type of assignment to the variable  ② the same variable, you can modify the variable's data type at multiple different assignments: var width = 10;//width is the shaping variable width= "haha";//width is changed to string type   The ③ variable can be declared with Var, or you can omit var. [difference] does not use VAR, the default is the global variable  ④ the same variable name, can be declared with Var multiple times. But there is no meaning and no error. The declaration after the second time will only be interpreted as assignment;  2, variable naming specification: ① variableName, can only have letters, numbers, underscores, $ composition ② The beginning cannot be a number ③ variable case-sensitive, uppercase and lowercase letters for different variables  3, variable names to conform to the hump rule: The variable starts with lowercase, followed by the first letter of each word (or the words are separated by an underscore) The data type of the variable in jianghaoisshuaige√jiang_hao_is_shuai_ge√jianghaoisshuaigex 4, JS undefined: Undefined, variable declared with Var, No initialization assignment was made. var A; Null: Represents an empty reference. For example, an empty object, an empty array. Boolean: True or FALSE, optional value true/falsenumber: numeric type. Can be a decimal, or an integer. String: Type. Use "" or "wrapped content to become a string. Object (complex data type): Subsequent explanations, functions, arrays, etc...  5, commonly used numeric functions: ①isnan (): Used to determine whether a variable or constant, if it is Nan (non-numeric) use IsNaN judgment, will attempt to use the number () function to convert, if it can be converted to numbers, then not a non-numeric, the result is false "111" Pure numeric string, false "" empty string, false "1a" contains other characters, Truetrue/false Boolean type, False ②number (): Converts other types to numeric types [string type to numeric]>>> The string is a pure numeric string and will be converted to the corresponding number "111"->111>>> string is an empty string, which will be changed to 0 ""->0>>> string containing other non-numeric characters, cannot convert "111a"->nan  [Boolean Boolean type to numeric]true, 1 false, 0 [null/undefined to numeric]null, 0 Undefined, nan [ The object type goes to the numeric value]* (later) calls the ValueOf method, determines whether the function has a return value, and then judges according to the various conditions described above.  ③parseint: Convert string to numeric type >>> empty string, cannot go. The result is a nan>>> pure numeric string, which can be turned. "123", "123" "123.5", 123 (decimal conversion, directly erase the decimal point,Not rounded) >>> strings that contain other characters. The numeric part before the first non-numeric character is intercepted. "123a456", 123 "a123b456", nan >>> parseint can only be converted to string type, boolean/null/undefined are Nan ④ parsefloat: Convert string to numeric >>> use same as parseint. However, when the decimal string is converted, the decimal point is preserved, and when the integer string is converted, the integer is reserved; "123.5", 123.5 "123", 123 ⑤typeof: Used to detect the variable data type undefined Undefined string-& Gt Output statements in Function [js, String true/false, boolean value,/null, Object function] document.write (); Output statement, print the contents of write () on the browser screen;  use note: Anything other than variables/constants must be placed in "" when printing. Variables/constants must be placed outside the "". When the printed content is made up of multiple parts, use a + link between them: For example: document.write ("Solitaire in the left hand:" +left+ "<br/>"),  alert (), pop-up Warning, () using in, Ibid.    

 1. Arithmetic Operations+ Plus, minus, * multiplication,/Except,% take-up, + + self-increment 、--minus +: There are two functions, link string/addition operation; When the + both sides are numbers, the addition is performed, and when the + sides have either side of the string, the string is linked, the result of the link is considered a string; The variable is +1;--on the original basis: the self-decrement operator, the variable on the original basis-1; "The similarities and differences between n++ and ++n" n++: First, the value of n is used to calculate, then n+1;++n: first the value of N +1, and then n+1 after the value, to calculate, the same point: whether n++ or ++n , after executing the code, will be n+1;eg:var a = 3;var b,c;b = a++ +2; A=3 b=5 a=4c = ++a +2; A=4 a=5 c=7 2, assignment operation = Assignment, + = = *=/=%= + =: A + = 5; equivalent to a = a + 5; But the efficiency of the former is faster than that of the latter. 3, relational operation = = equals, = = =, = = is not equal to, = = is not equals, >, <, >=, <= = =: strictly equals; Type is different, return false; Type is the same, then next judgment; = = equals; Type is the same, same as = =; Try to convert the two sides of the equation to a number, then judge; exception: null = = Undefined√null = = = = = = UNDEFINEDX4, conditional operator (multi-mesh) A>b?true:false has two key symbols:? And: When? The preceding section evaluates to True when executing: the preceding code; When the previous part evaluates to False, the following code is executed: the >>> multi-mesh operator can be nested in multiple layers: for example: var Jieguo = num>5? " Input too big ":(num==5?" It's right! ":" Input is too small "); 5, logical Operators && and, | | Or! Non-[operator's priority] ()! + +--%/*+ < >= <===!=&&| | various assignments = + = *=/=%=Calculator Exercises

As follows

Membership card Consumption settlement

(Narcissus number) rebate problem

"If-else structure"1. Structure: if (judging condition) {//condition is true when executing}else{//condition is false when executing}2, if () expression, the result of the operation should be: ①boolean:true true false ②string: Non-empty string is true The empty string is False ③number:0 false everything is not 0, are true ④null/nan/undefined: all false ⑤object: All is true 3, else{} structure, can be omitted according to the specific circumstances;"Nested IF Structure"1. Structure: if (condition i) {//Condition one) {//Condition II) {//conditions set up && Condition II also established}else{//condition one set up && condition two not set up}}else{//condition one not established} 2, if structure can be multiple nested , but in principle no more than 3 floors"Switch-case structure"1. Structure notation: switch (expression) {case constant expression 1: statement 1;break;case constant Expression 2: statement 2;break;......default: statement nbreak;} 2. Note: The expression in ①switch (), and the expression following each case, can be any data type supported by JS (object and array not); All constant expressions after ②case must be different, otherwise only the first one will be executed; ③ The constants after the case can be any data type, and the different cases of the same switch structure can be a variety of different data types; When ④switch is judged, it uses the congruent judgment = = =. ⑤break: After executing the case code, jump out of the current switch structure; the consequence of a lack of break: start with the correct case and execute all case and default in turn. Reason: ⑥↓⑥switch structure in judgment, will only judge the correct answer, when the correct case is encountered, will not judge the subsequent project. Execute in turn. ⑦switch structures are executed faster than multiple if structures.   In a multi-channel branch, you can prioritize the use of the switch structure. Case 1: Calculator problem

Case 2: The problem of moving tables

Case 3: Maximum number problem

While LoopGeneral syntax while (loop condition) {} Step 1. Declare a loop variable 2. Determine the loop condition 3. Execute loop Body (code in {}) 4. Update the loop variable and then loop through step 2.3.4. The expression in while (), supported data type ①boolean:true true false ②string: Non-empty string for vacuum string for false ③number:0 for false everything non 0, all true ④null/nan/undefined: false ⑤object : All true "For loop" 1, for Loop has three expressions, respectively: ① define the loop variable ② judge the loop condition ③ update the loop variable three expressions between, use; For loop three expressions can be omitted, two, integral 2, for loop characteristics: First judge, then execute, 3, for loop three expressions, can have multiple parts, separated by commas; however, the second part of the judging conditions need to use && link, the final result needs to be true/false * /Key Case 1. Fibonacci-Cut Series

Case 2. Guessing numbers

Case 3. Lian Addend

Enter 5 to

Case 4: Yang Hui triangle

In general, JS is a need to take seriously, seriously learn the language, and June mutual encouragement, joint efforts.

JS Preliminary Study

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.