Python JavaScript overview, pythonjavascript
I. How to compile it? 1. JavaScript code format
<! DOCTYPE html>
2. JavaScript storage location
The script in HTML must be located between the <script> and </script> labels.
Scripts can be placed in the <body> and
Because the Html code is executed from top to bottom, if the js Code in the Head is time-consuming, the user will be unable to see the page for a long time. If it is placed at the bottom of the body code block, even if the js Code is time-consuming, the page effect will not be affected, but the js effect is slow.
Here, we recommend that you put it at the bottom of the body;
3. Notes
JavaScript does not execute comments.
We can add comments to explain JavaScript or improve the readability of the Code.
A single line comment starts.
// Output title: document. getElementById ("myH1 "). innerHTML = "Welcome to my homepage"; // output section: document. getElementById ("myP "). innerHTML = "this is my first paragraph. ";
Multi-line comments start with/* and end.
/* The following code will output a title and a paragraph, which will represent the beginning of the home page */
Ii. Variables
The declaration of variables in JavaScript is a very error-prone point. Local variables must start with a var. If var is not used, global variables are declared by default.
JavaScript variables can be used to store values (such as x = 5) and expressions (such as z = x + y ).
var x=5;var y=6;var z=x+y;
Variables can use short names (such as x and y), or better descriptive names (such as age, sum, totalvolume ).
- The variable must start with a letter.
- Variables can also start with $ and _ (but we do not recommend this)
- Variable names are case sensitive (y and Y are different variables)
Both JavaScript statements and JavaScript variables are case sensitive.
<Script type = "text/javascript"> // global variable name = 'seven'; function func () {// local variable var age = 18; // global variable gender = "male"} </script>
Iii. Data Types
The data types in JavaScript are divided into the original type and object type:
- Original Type
- Number
- String
- Boolean Value
- Object Type
1. Number
The integer and floating point values are not distinguished in JavaScript. All numbers in JavaScript are represented by floating point values.
Conversion:
- ParseInt (...) converts a value to a number. If it fails, NaN
- ParseFloat (...) converts a value to a floating point number. If it fails, NaN
Special Value:
- NaN, not a number. You can use isNaN (num) to determine.
- Infinity and Infinity. You can use isFinite (num) to determine.
2. String
A string is an array composed of characters, but in JavaScript the string is immutable: You can access text anywhere in the string, but JavaScript does not provide a method to modify the content of a known string.
Common functions:
Obj. length get the length of the current string
Obj. trim () Remove Blank
Obj. trimLeft ()
Obj. trimRight)
Obj. charAt (n) returns the nth character in the string
Obj. concat (value,...) stitching
Obj. indexOf (substring, start) subsequence position
Obj. lastIndexOf (substring, start) subsequence position
Obj. substring (from, to) obtain the sub-Sequence Based on the index-starting position and ending position
Obj. slice (start, end) Slicing
Obj. toLowerCase () Capital
Obj. toUpperCase () lower case
Obj. split (delimiter, limit)
Obj. search (regexp) starts from scratch and returns the first position (g is invalid) after successful match)
Obj. match (regexp) global search. If the regular expression contains g, all are found. Otherwise, only the first is found.
Obj. replace (regexp, replacement) replace. If there is g in the regular expression, replace all; otherwise, replace only the first match,
$ Number: match the nth group content;
$ &: Currently matched content;
$ ': The text on the left of the matched substring;
$ ': Text on the right of the matched substring
$: Direct quantity $ symbol
3. boolean type
The boolean type only contains true and false characters. It is different from Python in lower case.
- = Equal comparison value
- ! = Not equal
- === The comparison value and type are equal
- ! = Not equal
- | Or
- &
4. Array
Arrays in JavaScript are similar to lists in Python.
Common functions:
Obj. size of the length array obj. append element obj at the end of push (ele. get an element obj at the end of pop. insert the element obj in the unshift (ele) header. shift () removes the element obj from the header. splice (start, deleteCount, value ,...) insert, delete, or replace the array element obj. splice (n, 0, val) specifies the position to insert the element obj. splice (n, 1, val) specifies the position to replace the element obj. splice (n, 1) specifies the position to delete the element obj. slice () slice obj. reverse () reverse obj. join (sep) concatenates array elements to construct a string obj. concat (val ,..) connects to the array obj. sort () sorts array elements
IV. for Loop and Condition Statement
1. for Loop
During the loop, the element of the loop is the index.
<!DOCTYPE html>
Execution result: