JavaScript is a programming language, the browser has built-in JavaScript interpreter, so in the browser according to the JavaScript language rules to write the corresponding code, the browser can explain and make corresponding processing.
First, the Code storage location
JavaScript code should be stored in:
- In the head of HTML
- The bottom of the body code block of HTML (recommended)
Because the HTML code is executed from top to bottom, if the JS code in the head is time consuming, it will cause the user to 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, it will not affect the user to see the page effect.
<script src= "https://www.gstatic.com/og/_/js/k=og.og2.en_US.iF4jnkQuaf0.O/rt=j/t=zcms/m=def/exm=in,fot/d=1/ Ed=1/rs=aa2yrtv5-poc4ks9gtgrdy2ywuwisqz7-q "></script><script> alert (' 123 ');</script>
Two forms of existence:
<!--way one--><script type "Text/javascript" src= "js file" ></script> <!--way two--><script Type "Text/javascript" > JS Code content </script>
Second, global variables and local variables
The variable that starts with Var in javascript is a local variable, and a global variable that starts with no var.
Python is a big difference from JavaScript by declaring a local variable and then using the Global keyword to program the variables globally.
<script type= "Text/javascript" > //global variable name = ' Seven '; function func () { //local variable var age =; global variable gender = "Male" }</script>
Code comments in JavaScript (this note is only valid in a script code block):
- Single-line//
- Multi-Line/* * *
Third, the data type
The data types in JavaScript are divided into primitive types and object types:
Original type:
- Digital
- String
- Boolean value
Object type:
Two special data types are null, undefined
- Null is a keyword in the JavaScript language that represents a special value that is commonly used to describe "null values".
- Undefined is a special value that indicates that the variable is undefined.
In particular, numbers, booleans, nulls, undefined, and strings are immutable.
It is important to note that the JavaScript string is immutable (immutable), and the method defined by the string class cannot alter the contents of the string. A method like String.touppercase () returns a completely new string instead of modifying the original string.
, the string class in JavaScript is similar to the following case, when the string is created, the memory length is specified, and the length is not modifiable and cannot be changed.
The list dictionary in JavaScript is connected to different memory locations in the form of linked lists, and the list dictionary length can be modified by a variable
1. Numbers (number)
In JavaScript, integer values and floating-point values are not distinguished, and all numbers in JavaScript are represented by floating-point numbers.
Transformation:
- parseint (..) Converts a value to a number, or Nan if unsuccessful
- Parsefloat (..) Converts a value to a floating-point number, or Nan if unsuccessful
Special values:
- NaN, not a number. Can be judged using IsNaN (num).
- Infinity, infinitely large. Can be judged using isfinite (num).
More numerical calculations:
Constant MATH.E constant E, base of natural logarithm. The natural logarithm of the math.ln1010. The natural logarithm of the math.ln22. math.log10e the logarithm of E with a base of 10. MATH.LOG2E the logarithm of E with a base of 2. MATH.PI constant Figs/u03c0.gif. The square of the math.sqrt1_22 is eradicated with 1. The square root of the math.sqrt22. The static function Math.Abs () calculates the absolute value. Math.acos () calculates the inverse cosine value. Math.asin () calculates the inverse chord value. Math.atan () calculates the inverse tangent value. Math.atan2 () calculates the angle from the x-axis to a point. Math.ceil () rounds a number. Math.Cos () calculates the cosine value. Math.exp () calculates the exponent of E. Math.floor () to a number of people. Math.log () calculates the natural logarithm. Math.max () returns the larger of the two numbers. Math.min () returns the smaller of the two numbers. Math.pow () calculates XY. Math.random () calculates a random number. Math.Round () is rounded to the nearest integer. Math.sin () calculates the sine value. MATH.SQRT () calculates the square root. Math.tan () calculates the tangent value. Math
Math
2. Strings (String)
A string is an array of characters, but in JavaScript the string is immutable: You can access text anywhere in the string, but JavaScript does not provide a way to modify the contents of a known string.
Method of String:
obj.length length obj.trim () Remove Blank obj.trimleft () obj.trimright) Obj.charat (n) Returns the nth character in a string Obj.concat (value, ...) Stitching obj.indexof (substring, Start) sub-sequence position obj.lastindexof (Substring,start) Sub-sequence position obj.substring (from, to) Get sub-sequence Obj.slice (start, end) & by index nbsp; slicing obj.tolowercase () Capital Obj.touppercase () lowercase obj.split (delimiter, limit) split Obj.search (regexp) match from the beginning to return the first position where the match succeeded (G invalid) Obj.match (regexp) Global Search, If there is a G in the regular to find all, then only the first one is found. Obj.replace (regexp, replacement) Replace, there is g in the regular replaces all, otherwise only the first match is replaced, $ Number: Matches the nth group content; &Nbsp; $&: Current matching content; $ ': Text on the left side of the matched substring; $ ': Text on the right side of the matching substring $$: Direct Volume $ symbol
[Front-end notes] Article three: JavaScript