Basic JavaScript knowledge points

Source: Internet
Author: User
Tags array definition
Tag (space separation) variable 1. Variable variable is a container for storing book values; JavaScript Learning

Tags (separated by spaces) variables

1. What is a variable?
A variable is a container that stores book values;

2. Game Rules
Variable naming: a variable can contain letters, numbers, underscores (_), and dollar signs ($.

1. It must begin with a letter, underscore, or dollar sign. It can be followed by letters, underscores, dollar signs, and numbers.

2. Variable names are case sensitive, such as myvar and myVarhi.

3. JavaScript keywords and reserved words cannot 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 only have two types)

  • Array)

  • Object)

Undefined and null

Var mychar1 = "string enclosed in double quotes"; // This is the string var mychar2 = 'string enclosed in single quotes '; // This is also the string var mychar3 = 'garlic: "I like Xiao Ke in our class. "'; // Double quotation marks in the string. single quotation marks include var mychar4 =" Uncle Wang: "\", 'learn' to attract girls ~ \ ""; // Or use the \ symbol before a specific symbol (Quotation Mark) to escape the output var mynum1 = 6; // This is the number 6var mynum2 = 6.00; // This is also the number 6> var mynum3 = 123e; // This is the 12300000var mynum4 = 123e-5 written using the scientific (exponential) calculation method; // This is 0.00123var mynum5 = ture; // This Is A boolean value var mynum6 = [1, 2, 3]; // This is an array var myobject = {"p": "Hello"}; // This is an object


Basic Expressions and operators

1. Basic expression

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

Var y = "you"; var mysay = "I" + "love" + y; // = a string expression, the value of mysay is the string var mynum = 12 + 6*2; // = followed by a numerical expression, and the value of mynum is the value var mynum> 12; // = followed by a Boolean expression, the value of mysay is a Boolean value.

2. Operators

2.1 Arithmetic Operators
For example: +-8 */

Var num = 24; var myresult1 = + num % 4 + 6*2; // What is myresult? Var myresult2 = num % 4 + 6*2; // What is myresult?

2.2 value assignment operator

The arithmetic operator can be simplified before =. For example, num % = 4 is equivalent to num = num % 4.

2.3 comparison Operators
For example, >,<,>=, <=
= Equal
=== All equals
! = Not equal

2.4 logical operators
& (Series)
| (Parallel)

2.5 operator priority (high to low ):

-*/Arithmetic Operators

= <等比较操作符
& |! And other logical operators
= Copy the symbol.
If the operation at the same level is performed in the left-to-right order, the multi-layer parentheses are enclosed and outward.
Here, we will remind you that when you cannot tell the priority, you can remember the order of operation by adding brackets.

Exercise: link numbers and strings
Specify the following non-string results

Array

What is an array?

1. array Definition
One sentence: variables that can store multiple data

An array (Arry) is a group of values arranged in order. A single value is called an element and their positions are numbered (starting from 0, that is, the subscript of the first element is 0, the second is 1, and so on ). Square brackets represent the entire array.

// Expression 1 var arr = []; var arr [0] = 'a'; var arr = [1] = 'B '; var arr = [2] = 'C'; var arr = [3] = 'D '; // Expression Form 2 var arr = ['A' B 'C' d'];

2. What can I install?
Any data type can be put into the array.

var arr=['x',{a:1},[1,2,3], fucation(){return true;}];arr[0];  //stringarr[1];  //Objectarr[2];  //Arrayarr[3];  //fucation

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

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

The length attribute of an array differs from the length attribute of an object. As long as an array is used, the length attribute must exist, but the object may not.

In addition, 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=['a','b'];arr.length; //2arr[2]=;'c';arr.length;  //3arr[9]='d';arr.length;  //10arr[1000]='e';arr.lengh;  //10001

It can be found that the number key value of the array does not need to be continuous, and the value of the length attribute is always equal to the maximum key value of 1.

The 3.2 length attribute is writable. If you manually set the number of members of a light rain, the number of members of the array is automatically reduced to the 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 maximum integer can only be 1, so the elements ('C') corresponding to key value 2 are automatically deleted. Therefore, an effective way to clear an array is to set the length attribute of the array to 0.

3.3 array Length

Note that the index of an array always starts from 0, so the upper and lower limits of an array are: 0 and length-1. For example, 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. array assignment

Var myarr = new Array (3); myarr [0] = "5th"; myarr [1] = "James"; myarr [2] = "console"; console. log ("the student ID in the class is 0:" + myarr [0]); console. log ("the student ID in the class is 1:" + myarr [1]); console. log ("the student ID in the class is 2:" + myarr [2]); var arr = ["1", "abc", "myarr"]; console. log (arr [1]);

6. Add new elements

Myarr [0] = "5th"; myarr [1] = "James"; myarr [2] = ""; console. log ("the student ID in the class is 0:" + myarr [0]); console. log ("the student ID in the class is 1:" + myarr [1]); console. log ("the student ID in the class is 2:" + myarr [2]); myarr [3] = ""; console. log (myarr [3]); myarr [0] = "5th"; myarr [1] = "James"; myarr [2] = ""; console. log ("the student ID in the class is 0:" + myarr [0]); console. log ("the student ID in the class is 1:" + myarr [1]); console. log ("the student ID in the class is 2:" + myarr [2]); myarr [3] = ""; console. log (myarr [3]);

7. Use array literal
To get the value of an array element, you only need to use an array variable and provide an index.

Var myarr = ["Xiao Lei", "Xiao Ke", "Xiao Xin", "Xiao Ming", ""]; var mynum = 4; console. log ("the student ID is 4" + myarr [mynum]);


8. Multi-dimensional array nesting

Var myarr = [[, 3], [, 3] myarr [0] [1] = 5; // input the value of 5 into the array to overwrite the original value. Console. log (myarr [0] [1]);

Knowledge Point Extension
Simple for loop:

Var arr = ['A', 'B', 'C']; for (var I = 0; I

The above is the content of the basic JavaScript knowledge point. For more information, see the PHP Chinese website (www.php1.cn )!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.