Home >Web Front-end >JS Tutorial >JavaScript basic knowledge points
JavaScript learning
Tag (space separated) variable
1. What is a variable
A variable is a container that stores book values;
2 .Game Rules
【Variable Naming】Variables can be composed of letters, numbers, underscores (_) or dollar signs ($).
1. Must start with a letter, underscore, or dollar sign, and can be followed by letters, underscores, dollar signs, and numbers.
2. Variable names are case-sensitive, such as: myvar and myVarhi are two different variables.
3. JavaScript keywords and reserved words are not allowed to be used as variable names, such as break and Boolean.
3. Variable declaration and assignment
var myvar=123;
4. Data type
string (string)
Nubmber (number)
Boolean (for example, true and false have only two types)
Array (array)
Object (object)
var mychar1="双引号包起来的字符串";//这是字符串 var mychar2='单引号包起来的字符串';//这也是字符串 var mychar3='小蒜:"我喜欢我们班的小可。"';//字符串中有双引号,用单引号包含 var mychar4="Uncle Wang:"\"小蒜啊,'学习好'才能吸引女孩哦~\""; //或者在特定符号(引号)前使用\符号,使其转义输出 var mynum1=6; //这是数字6var mynum2=6.00; //这也是数字6> var mynum3=123e;//这是使用科学(指数)计算法来书写的12300000 var mynum4=123e-5;//这是0.00123var mynum5=ture;//这是布尔值 var mynum6=[1,2,3];//这是数组 var myobject={"p":"Hello"};//这是对象
var y="you"; var mysay="I"+"love"+y; //=后面是串表达式,mysay值是字符串 var mynum=12+6*2;//=后面是数值表达式,mynum值是数值 var mynum>12;//=后面是布尔表达式,mysay值是布尔值2. Operation Symbol 2.1 Arithmetic operator
For example: +-8*/
var num=24; var myresult1=++num%4+6*2;//myresult是多少呢? var myresult2=num%4+6*2;//myresult是多少呢?2.2 Assignment operator It can be simplified by placing the arithmetic operator before =. For example, num%=4 is equivalent to num=num%4. 2.3 Comparison operators
For example:>,6580843315dd7804e35fd3743df832ea=,<=
==equal to
===all equal to
! = is not equal to
&& (series connection)
|| (parallel connection)
Indicate the following non-string result
One sentence understanding: Variables that can store multiple data
//表达形式一var arr=[]; var arr[0]='a'; var arr=[1]='b'; var arr=[2]='c'; var arr=[3]='d';//表达形式二 var arr=['a''b''c''d'];2. What can be installed?
Any type of data can be put into an array.
var arr=['x',{a:1},[1,2,3], fucation(){return true;}]; arr[0]; //stringarr[1]; //Objectarr[2]; //Arrayarr[3]; //fucationIt can be seen that the elements in the array can also be an array. We call this form a multi-dimensional array.
var arr=[[1,2],[3,4]]; arr[0][1]; //2arr[1][1]; //43.length attribute
3.1 The length attribute of the array can return the number of members of the array.
var arr=['a','b']; arr.length; //2arr[2]=;'c'; arr.length; //3arr[9]='d'; arr.length; //10arr[1000]='e'; arr.lengh; //10001It can be found that the numeric key values of the array do not need to be consecutive, and the value of the length attribute is always equal to the largest key value greater than 1. 3.2 The length attribute is writable. If you manually set a value for the current number of members in Xiaoyu, the members of the array will automatically be reduced to the length set by length.
var arr=['a','b','c']; arr.length; //3arr.length=2; arr; //['a','b']When the length attribute of the array is set to 2, that is, the largest integer can only be 1, so the element ('c') corresponding to the key value 2 is automatically deleted. Therefore, an effective way to clear an array is to set the length attribute of the array to 0. 3.3 The length of the arrayIt should be noted that because the index of the array always starts from 0, the upper and lower limits of an array are: 0 and length-1 respectively. If the length of the array is 5, the upper and lower limits of the array are doubled to 0 and 4. 4. Create an array
var myarr=new Array(6); console.log(myarray);5. Assign an array
var myarr=new Array(3); myarr[0]="小五"; myarr[1]="小明"; myarr[2]="月影"; console.log("班里学号为0的是:"+myarr[0]); console.log("班里学号为1的是:"+myarr[1]); console.log("班里学号为2的是:"+myarr[2]); var arr=["1","abc","myarr"]; console.log(arr[1]);6. Add a new element
myarr[0]="小五"; myarr[1]="小明"; myarr[2]="月影"; console.log("班里学号为0的是:"+myarr[0]); console.log("班里学号为1的是:"+myarr[1]); console.log("班里学号为2的是:"+myarr[2]); myarr[3]="小新"; console.log(myarr[3]); myarr[0]="小五"; myarr[1]="小明"; myarr[2]="月影"; console.log("班里学号为0的是:"+myarr[0]); console.log("班里学号为1的是:"+myarr[1]); console.log("班里学号为2的是:"+myarr[2]); myarr[3]="小新"; console.log(myarr[3]);7. Use an array literal
To get the value of an array element, just use the array variable and provide an index.
var myarr=["小雷","小可","小新","小明","月影"]; var mynum=4; console.log("学号为4的是"+myarr[mynum]);
var myarr=[[0,2,3],[1,2,3]] myarr[0][1]=5;//将5的值传入数组中,覆盖原有值。 console.log(myarr[0][1]);Knowledge point expansion
Simple for loop:
var arr=['a','b','c']; for(var i=0; i<arr.length; i++){ console.log(arr[i]); }The above is JavaScript Basic knowledge points, please pay attention to the PHP Chinese website (www.php.cn) for more related content!