Importing JavaScript1. Internal Label<script> alert("hello world"); </script> 2. External introduction<script src="js/abc.js"></script> Basic syntaxDefining variables <script> var num = 1; alert(num); </script> Condition Control if (2>1) { alert("true"); } <script> var score = 65; // alert(num); if (score>60&&score<70) { alert("60-70") } else if (score>70&&score<80) { alert(70-80) } else { alert("other") } </script>
Data Typesnumberjs does not distinguish between integers and decimals 123 //Integer 123 123.1 //Floating point number 123.1 1.122e3 //Scientific notation NaN //not a number Infinity //Infinity String'a' "abc" Normally, use single quotes or double quotes to wrap the string. Note the escape symbol \ \' \n //line break\t //space\u4e2d //unicode encoding\x41 //ascii To write a multi-line string, use backticks var str = `haha nihao 666` Template String let name='xay'; let words = `Hello, ${name}`; String length word.length The characteristics of strings are immutable Case conversion word.toUpperCase() word.toLowerCase() substring() is a string interception function substring(1) // extract from the first string to the last substring(1,3) //[1,3) Booleantrue false Logical operations&& //and| //or! //not Comparison Operators= == // Different types, same value is true === // Absolutely equal, both type and value must be the same NaN===NaN returns false and can only be judged by isNaN(NaN) Arrays<script> var arr = [1,2,3,4,5,'hello'] </script> When taking an array index, if it crosses the bounds, undefined will be output After assigning a value to arr.length, the length of the array will also change. If the value assigned is too small, the elements in the array will be lost. ObjectIn js, {…} represents an object. The key-value pair describes the attribute xxxxx:xxxxx. Multiple attributes are separated by commas, and the last attribute does not have a comma. var person = { name: 'xay', age: 18, tags: ['js','java','python'] } Object Assignment Dynamically delete the attribute delete person.name To add object properties, just assign values directly Determine whether the attribute is in the object Process ControlIf judgment if (2>1) { alert("true"); } <script> var score = 65; // alert(num); if (score>60&&score<70) { alert("60-70") } else if (score>70&&score<80) { alert(70-80) } else { alert("other") } </script> While Loop age=0; while (age<100) { age+=1; console.log(age); } for loop for (let i = 0; i < 5; i++) { console.log(i); } for loop iterates over an array var arr = [1,2,3,4,5,6,7,8,9,10]; for (var num in arr) { console.log(num) } Map and SetMap var map = new Map([['tom',100],['jack',90],['haha',80]]); var name=map.get('tom'); //Get value through key console.log(name) Similar to the dictionary in Python, set() adds data to the Map map.set('admin',10); map.delete('tom') //deletion in map Set Set can remove duplicates var set = new Set([3,1,1,1,1]); set.add(2) //Add set.delete(1) //Delete console.log(set.has(3)); //Is there 3? iteratorIterating over a Map var map = new Map([['tom',100],['jack',90],['haha',80]]); for (let x of map) { console.log(x); } Iterating over a Set var set = new Set([3,1,1,1,1]); for (let x of set) { console.log(x); } SummarizeThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Problem analysis of using idea to build springboot initializer server
>>: Notes on the MySQL database backup process
Install antd-mobile Global import npm install ant...
Preface: When we need to store decimals and have ...
I personally feel that the development framework ...
Table of contents Why update the auto-increment i...
Table of contents Vue2.x Usage Global Registratio...
Assume there are two Linux servers A and B, and w...
Table of contents What is the rest operator? How ...
MySql Index Index advantages 1. You can ensure th...
The effect of this function is similar to vue的pro...
This article uses examples to illustrate the impl...
Table of contents String length: length charAt() ...
Win2008 R2 zip format mysql installation and conf...
Table of contents Build a Docker image using Dock...
Use apk add ansible to add the ansible service to...
Linux remote deployment of MySQL database, for yo...