This is the 9th day of my participation in the August Wen Challenge.More challenges in August
Js writing position
- inline
<input type="button" value="Button" onclik="alert("hello js"/ >Copy the code
- Written in the script
<head>
<script>
alert("hello js")
</script>
</head>
Copy the code
- Write it in an external JS file and introduce it in the page
<script src="main.js"></script>
Copy the code
- Pay attention to
- Script cannot be written as a single label
- The loading order is top-down
annotation
- A single
/ / comment Copy the code
- Multiple lines
/ * * / Copy the code
Variables (master)
- The statement
var age
Copy the code
- The assignment
var age
age = 18
Copy the code
- Declare multiple at the same time
var age, name, sex
age = 10
name = 'you'
Copy the code
- Define variables
var age = 10
Copy the code
- Define and assign multiple variables simultaneously
var age = 10, name = 'you'
Copy the code
The data type
- Five basic data types
- Number: indicates the value type
- String: indicates the character type
- Bollean: Boolean type
- Undefined: Undefined type
- Null: empty type
- Get variable type (master)
typeof 1. typeofThe variable name2. typeof(Variable name)Copy the code
// typeof variable name/value console.log(typeof 123);//number console.log(typeof '123');//string console.log(typeof true);//boolean console.log(typeof undefined);//undefined console.log(typeof null);//object, the object Number type is also called a numeric type. Copy the code
The operator
- Arithmetic operator
//+ - * / % 3%2=1 var x = 10; var y = 5; console.log(x + y);/ / 15 console.log(x - y);/ / 5 console.log(x * y);/ / 50 console.log(x / y);/ / 2 console.log(10 % 3);/ / 1 Copy the code
- Unary operator
/ front + +var num1 = 8; ++num1;// add 1 to itself console.log(num1); / / rear + + var num1 = 5; num1++;// add 1 to itself console.log(num1); Copy the code
- conclusion
- Pre ++ : add 1 first, then participate in the operation
- After ++ : participate in the operation first, then add 1
- Once the top two are understood, the bottom two make sense
- Pre – : first minus 1, then participate in the operation
- Postposition — : participates in the operation first and then subtracts 1
Data type conversion
- To a numeric type
// use parseInt() // If the first character is a number or an operation symbol, parsing starts until it encounters a non-number character, stops parsing and gets the result console.log(parseInt("10"));/ / 10 console.log(parseInt("10abcdefg"));/ / 10 console.log(parseInt("g10"));//NaN console.log(parseInt("1fds0"));/ / 1 console.log(parseInt("10.98"));/ / 10 console.log(parseInt("10.98 abcdefg"));/ / 10 Copy the code
// parseFloat() function // Convert other types to floating point numbers console.log(parseFloat("10"));/ / 10 console.log(parseFloat("10abcdefg"));/ / 10 console.log(parseFloat("g10"));//NaN console.log(parseFloat("1fds0"));/ / 1 console.log(parseFloat("10.98.98"));/ / 10.98 console.log(parseFloat("10.98 abcdefg"));/ / 10.98 Copy the code
// the Number() function // You can convert any value to a numeric type, and if there is a non-numeric character in the string to be converted, the result is NaN console.log(Number("10"));/ / 10 console.log(Number("10abcdefg"));//NaN console.log(Number("g10"));//NaN console.log(Number("1fds0"));//NaN console.log(Number("10.98"));/ / 10.98 console.log(Number("10.98 abcdefg"));//NaN Copy the code
//+, -0 var strNum = '18'; console.log(+strNum); / / take is 18 console.log(-strNum); / / to 18 take negative console.log(strNum - 0); // 18 strNum is converted to a numeric type before subtraction console.log(strNum + 0); // 180 is converted to a string type before concatenation Copy the code
Note that the plus sign and the minus sign are placed before the variable, respectively, to take the positive and negative effect; Subtract 0 from a variable. By default, convert the variable to a numeric type and subtract 0 from the variable.Copy the code
- To a string type
toString()
(master)Stirng()
(understand)
Note that the toString() method can only convert numeric and Boolean types to strings, not Undefined and Null. The String() function converts all primitive types to stringsCopy the code
-
To a Boolean type
- Take the five special values of false
""(Blank string)0(save0 -0)undefined null NaN Copy the code
- The others are true
- Take the five special values of false
-
The operator
- The priority of the operator
Priority from high to bottom1.() has the highest priority2.The unary operator ++ --!3.The arithmetic operators are preceded by * / % and followed by + -4.The relational operator (compare sizes) > >= < <=5.Equality operator (compare equality) ==! = = = =! = =6.Logical operators && before | |7.Assignment operator = += -= *= /= %=Copy the code
- Ternary operator
Ex. :var age = 18 var result = age >= 18 ? "Come of age" : "Minor" console.log(result) Example: Find the maximum value of two valuesvar num1 = 8 var num2 = 2 var max = num1 > num2 ? num1 : num2 console.log(max) Copy the code
-
An array of
- Through the array
for(var i = 0; i < arr.length; i++){ }Copy the code
- An array of practice
Example 1: Find the sum of all elements in an array var arr1 = [10.20.30.40.50]; var sum = 0; for(var i = 0; i < arr1.length; i++){ sum += arr1[i]; } console.log(sum);/ / 150 // Example 2: Find the average of all elements in the array var arr2 = [1.2.3.4.5]; var sum2 = 0; for(var i = 0; i < arr2.length; i++){ sum2 += arr2[i]; } console.log(sum2 / arr2.length);/ / 3 Copy the code