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值是布尔值
For example: +-8*/
var num=24; var myresult1=++num%4+6*2;//myresult是多少呢? var myresult2=num%4+6*2;//myresult是多少呢?
For example:>,<,>=,<=
==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'];
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]; //fucation
var arr=[[1,2],[3,4]]; arr[0][1]; //2arr[1][1]; //4
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; //10001
var arr=['a','b','c']; arr.length; //3arr.length=2; arr; //['a','b']
var myarr=new Array(6); console.log(myarray);
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]);
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]);
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]);
Simple for loop:
var arr=['a','b','c']; for(var i=0; i<arr.length; i++){ console.log(arr[i]); }