I. Introduction to JavaScript
"1" is an object-and driver-based language that is applied to the client.
1. Object-based: Provides good objects that can be used directly.
2. Event-driven: HTML makes Web site static effect, JavaScript does dynamic effect.
3. Client: specifically refers to the browser.
Features of "2" JS
1. Interactivity: Dynamic interaction of information.
2. Security: JS cannot access the files on the local disk.
3. Cross-platform: As long as the JS-enabled browser can run the JS script.
The difference between "3" JS and Java
1.javascript is Object-based, and Java is Object-oriented.
2.java is a strongly typed language, JavaScript weakness type language.
Part of "4" JS
1.ECMAScript:
ECMA: The European Computer Association, which defines the standard syntax for JS.
2.BOM: Browser object Model (broswer)
3.DOM: Document Object Model (doucument)
Second, the combination of JS and HTML way
"First" Using tags <script type= "text/javascript" ></script>
alert ("JS and HTML combination One"); </script> </body>
"Second type" use <script> tag, introduce external JS file
C. JS primitive type and declaring variable
The keyword used to declare variables in JS is: Var
"1" Primitive type
1.string: String
2.number: Number Type
3.boolean:true and False
4.null: Indicates that the reference to the object is empty and that all objects are references to object
5.undifined: A variable is defined, but not assigned, for example: Var B;
"2" typeof (), you can view the data type of the current variable.
//declaring a string varstr = "123"; //declaring number Types varnum = 123; varType =typeof(num); alert (type);//Result: number //declaring a Boolean type varb =true; </script> </body>
Four, JS in the control of the statement
"1" If judgment statement
in JavaScript, not an empty string, a number that is not 0, a non-null object, not a undefined object property, Boolean True, and when they are alone as an expression in a judgment, JS determines that they are true, and vice versa is false.
"2" switch statement
"3" loop statement for, while, Do-while
//If judgment statement varb = 6; if(b = = 6) {alert ("The value of variable B is:" +b); } Else{alert ("Other"); } //Switch Statement varA = 3; Switch(a) { Case3: Alert (3); Break; Case4: Alert (4); Break; default: Alert ("Other"); } //For loop variable for(vari=0;i<3;i++) {alert (i); } </script> </body>
The operators in JS
The operators in "1" JS are basically the same as those in Java.
"2" notice a few details
Integers and decimals are not distinguished in 1.js
2. The addition and subtraction of the string, if you do the addition, do is the string connection operation, if you do subtraction, do is subtraction operation.
3.NaN means not a number.
The 4.boolean type can also be added, subtracted, and manipulated. If set to True, the value is equal to 1, and if set to False, the value is equivalent to 0.
"3" introduced knowledge: document.write (), is directly want to page output statements, you can want to page output variables, fixed values and HTML code.
varj = 123; /** * The result of j/1000*1000 in Java is 0. But in JS is different, * in JS does not distinguish between integers and decimals*/Alert (J/1000*1000); Results 123varFlag =true; Alert (Flag+ 1);//Results 2alert (flag-1);//Results 0 //addition and subtraction of strings varb = "123"; Alert (b+ 1);//result 1231alert (b-1);//Result 121</script> </body>
Six, JavaScript print multiplication table on the page
for (var i = 1; i < i++) { for (var j = 1; J <= I; j ++) {
+ "x" + i + "=" + J * i); document.write (" " ); } document.write ("</br>"); } </script> </body>
The array in JS
1. The first type: var arr = [1,2,3] var arr2 = [1, "AB", 4]
2. The second type: Use JS built-in object array
var arr = new Array (2); indicates that an array is defined that has a length of 2
arr[0] = 1;arr[2] = 2;
3. Third type: Use JS built-in object array
var arr = new Array (All-in-one) means an array is defined and the elements inside the array are 1, 2, 3
: (1) The array has the Length property, You can see the length of the array (2) The length of the array can be changed (3) The elements in the array that can hold different classes
The definition of functions in the eight and JS
Here are just two common ways to do this:
"1" Using the keyword function
// calling functions Test (4,3); // Defining Functions function Test (b) { alert (a+b); } </script> </body>
"2" anonymous function
// Defining Functions var function (A, a ) {- b); } // calling functions Sub (5,1); </script> </body>
Global variables and local variables in the nine, JS
"1" global variable: Define a variable in the script tag, which can be used in the JS section of the page. (used outside of a method, used inside a method, in another
Use in script tags)
"2" local variable: Defines a variable inside a method that can only be used inside a method.
"3" Add one point:
It is recommended to put <script> tags behind <body> , why is it recommended? If there is a need now: in JS to get the value of input, if the script is placed in the
Head inside is a problem, the parsing of HTML is from top to bottom parsing, script tag put into head inside, directly using JS to input value is not taken, because the page has not resolved to input this line.
var v = document.getElementById (" name "); alert (v.value); </script>
the value of input cannot be obtained at this time.
JavaScript Learning notes (i)