Home > Web Front-end > JS Tutorial > JavaScript basic knowledge points

JavaScript basic knowledge points

黄舟
Release: 2017-02-07 14:41:56
Original
1038 people have browsed it

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;
Copy after login

4. Data type

  • string (string)

  • Nubmber (number)

  • Boolean (for example, true and false have only two types)

  • Array (array)

  • Object (object)

##undefined and null

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"};//这是对象
Copy after login


Basic expressions and operators

1. Basic expression

In JavaScript, when + is used to connect strings, other variables will also be converted into strings for connection~

var y="you";
var mysay="I"+"love"+y; //=后面是串表达式,mysay值是字符串
var mynum=12+6*2;//=后面是数值表达式,mynum值是数值
var mynum>12;//=后面是布尔表达式,mysay值是布尔值
Copy after login

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是多少呢?
Copy after login

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:>,<,>=,<=
==equal to
===all equal to
! = is not equal to

2.4 Logical operators

&& (series connection)
|| (parallel connection)

2.5 Operator precedence (high to low):

-* /etc. arithmetic operators

= && || ! Logical operators such as
= copy symbol.
If operations at the same level are performed from left to right, multi-level brackets are from inside to outside.
As a reminder, when you can't tell the priority, just add parentheses to remember the order of operations.

Exercise: Link numbers and strings

Indicate the following non-string result

Array

What is an array

1. Definition of array

One sentence understanding: Variables that can store multiple data

Array (Arry) is a set of values ​​arranged in order. A single value is called an element, and their positions are numbered (starting from 0 as well) That is to say, the index of the first element is 0, the second element is 1, and so on). The entire array is represented by square brackets.

//表达形式一var arr=[];
var arr[0]=&#39;a&#39;;
var arr=[1]=&#39;b&#39;;
var arr=[2]=&#39;c&#39;;
var arr=[3]=&#39;d&#39;;//表达形式二
var arr=[&#39;a&#39;&#39;b&#39;&#39;c&#39;&#39;d&#39;];
Copy after login

2. What can be installed?

Any type of data can be put into an array.

var arr=[&#39;x&#39;,{a:1},[1,2,3], 
fucation(){return true;}];
arr[0];  //stringarr[1];  //Objectarr[2];  //Arrayarr[3];  //fucation
Copy after login

It 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];  //4
Copy after login

3.length attribute

3.1 The length attribute of the array can return the number of members of the array.

The length attribute of an array is different from the length attribute of an object. As long as it is an array, it must have a length attribute, but the object may not have it.

Moreover, the length attribute of the array is a dynamic value, which is equal to the maximum value in the key name plus 1.

var arr=[&#39;a&#39;,&#39;b&#39;];
arr.length; //2arr[2]=;&#39;c&#39;;
arr.length;  //3arr[9]=&#39;d&#39;;
arr.length;  //10arr[1000]=&#39;e&#39;;
arr.lengh;  //10001
Copy after login

It 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=[&#39;a&#39;,&#39;b&#39;,&#39;c&#39;];
arr.length;  //3arr.length=2;
arr;    //[&#39;a&#39;,&#39;b&#39;]
Copy after login

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 array

It 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);
Copy after login

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]);
Copy after login

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]);
Copy after login

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]);
Copy after login


8. Multidimensional array nesting

var myarr=[[0,2,3],[1,2,3]]
myarr[0][1]=5;//将5的值传入数组中,覆盖原有值。
console.log(myarr[0][1]);
Copy after login

Knowledge point expansion

Simple for loop:

var arr=[&#39;a&#39;,&#39;b&#39;,&#39;c&#39;];
for(var i=0; i<arr.length; i++){
console.log(arr[i]);
        }
Copy after login
The above is JavaScript Basic knowledge points, please pay attention to the PHP Chinese website (www.php.cn) for more related content!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template