Home > Web Front-end > JS Tutorial > body text

JavaScript introductory study guide for beginners

伊谢尔伦
Release: 2017-06-16 10:05:51
Original
2265 people have browsed it

Javascript is a must-learn course for front-end development. The following are the first three courses for getting started. Let’s take a look.

Lesson 1
1: Main features of javascript
Interpreted: no need to compile, the browser directly interprets and executes
Object-based: we can use JS directly Objects that have been created
Event-driven: Can respond to client input in an event-driven manner without going through the server-side program
Security: Access to the local hard disk is not allowed, and data cannot be written to the server
Cross-platform: js depends on the browser itself and has nothing to do with the operating system

Lesson 2
How to write Javascript in the webpage
1: Embed Javascript directly in the page

javascript can be inserted in the middle of the tag,
can also be placed in the in the middle of tags
Most commonly placed between tags

The case is as follows, insert the javascript code in in the middle of the label.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>初学javascript</title>
    <script language="javascript">        var now=new Date();//获取Date对象的一个实例
        var hour=now.getHours();//获取小时数
        var min=now.getMinutes();//获取分钟数
        alert("当前时间"+hour+":"+min+"\n欢迎访问柠檬学院http://www.bjlemon.com/");    </script>
</head>
<body>
</body>
</html>
Copy after login

Case 2 code is as follows

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>我的年月日</title>
    <script language="javascript">        var now=new Date();//获取日期对象
        var year=now.getYear()+1900;//获得年,在js中年份需要加1900才可以显示此时此刻的年份
        var month=now.getMonth()+1;//获得月份,月份是0-11,所以在js中需要加1
        var date=now.getDate();//获得日
        var day=now.getDay();//获得星期几
        var day_week=new Array("礼拜日","礼拜一","礼拜二","礼拜三","礼拜四","礼拜五","礼拜六");        var week=day_week[day];        var time="当前时间:"+year+"年"+month+"月"+date+"日"+week;
        alert(time);    </script>
</head>
<body></body>
</html>
Copy after login

2: Reference to external Javascript

If the script is more complex or the same code is used by many pages, you can use these scripts The code is placed in a separate file with the extension .js, and then the javascript file can be linked to the web page where the code needs to be used


(Recommendation) It is generally better to write the above code in the middle of

In the file with the .js suffix , there is no need to use <script></script> tags to enclose

means that the getDate() method getdate() is called when the page is loaded. It is a method defined in a file with a .js suffix

The suffix in this case is .html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>引用外部的js</title>
    <script language="javascript" src="js1.js">
    </script>
</head>
<body onload="getdate()">
</body>
</html>
Copy after login

The suffix in this case is .js

function getdate(){    var now=new Date();//获取日期对象
        var year=now.getYear()+1900;//获得年,在js中年份需要加1900才可以显示此时此刻的年份
        var month=now.getMonth()+1;//获得月份,月份是0-11,所以在js中需要加1
        var date=now.getDate();//获得日
        var day=now.getDay();//获得星期几
        var day_week=new Array("礼拜日","礼拜一","礼拜二","礼拜三","礼拜四","礼拜五","礼拜六");        
        var week=day_week[day];        
        var time="当前时间:"+year+"年"+month+"月"+date+"日"+week;
        alert(time);
    
}
Copy after login

Lesson 3
javascript Syntax
1: JavaScript syntax
1.1: JS variables are case-sensitive
Usename, useName are two different variables
1.2: The semicolon at the end of each line is optional. If there is no semicolon at the end of the statement, then js
will automatically use the end of this line of code as the end of the statement
  alert("hello world");
    alert("hello world")
1.3 : The variable is a weak type
Only use the var operator when defining a variable
For example: var usename="biexiansheng";
var age=22;
1.4: Use braces to label code blocks
{ //Code} Statements encapsulated in curly brackets are executed in order
1.5: Comments
1.5.1: Single-line comments //
Single-line comments start with a double slash "//" and end with " //"The text following is the comment content
The content of the comment has no effect during code execution.
      var now=new Date();//Get the date object
  1.5.2: Multi-line comment /**/
Multi-line comments start with /* and end with*/ ends, the content between the two is the comment content
                                               It has no effect during code execution.
/*
*Function: Get the current date
*Author: biexiansheng
*/
Function getClock () {
// content

Lesson 4
Data types of JavaScript (no matter how many data types there are in JavaScript, you can only use var when declaring)
1: Numeric type
Integer: 123 //Decimal
                                 0123                                                                                                                                                                                                              Use scientific notation
3.1415926 //Standard form of floating point number
3.14E9 //Use scientific notation to represent 3.14 times 10 raised to the 9th power
           
2: Character Type
Character data is one or more characters enclosed in single or multiple quotes
For example: 'a' 'hello world'
'a' "hello world"
None in javascript char data type
If you want to represent a single character, you must use a string of length 1

Single quotes include double quotes '"hello"'
Double quotes include single quotes "'world'"
3: Boolean type
Boolean type data only has true or false. In js, you can also use the integer 0 to represent false, and use a non-0 integer to represent true

4: Escape characters
Non-displayable special characters starting with a backslash are often called control characters, also known as escape characters
\bBackspace \nLine feed \fPage feed \tTab character \'Single quote \" Double quote \ \Backslash

5: Null value
null, used to define empty or non-existent references
For example, var a=null;

6: Undefined value
Variables that have been declared but not assigned a value
var a;
alert(a);
Pop up undefined is a keyword used to represent undefined values ​​

7:Array Type

Array type, an array is a sequence containing basic and combined data. In the JavaScript scripting language
Each data type corresponds to an object, and the data is essentially an Array object.
var score=[. 45,56,45,78,78,65];
Since the array is essentially an Array object, the operator new can be used to create a new array, such as
var score=new Array(45,65,78 ,8,45);
Accessing a specific element in the array can be achieved through the index position of the element, as shown in the following statement
The variable returns the 4th element in the array score
var m=score[3 ];

Lesson 5

Definition and use of variables

1: Naming rules for variables
Variable names consist of letters, numbers, and underscores, but cannot start with numbers
No Use keywords in javascript
Strictly case-sensitive
For example username username
2: Variable declaration
var variable
You can use one var to declare multiple variables, such as
var now ,year,month,date;
You can assign a value to a variable while declaring it, that is, initialize it
var now="2016-8-11",year="2016",month="8", date="11";
If you just declare a variable without assigning a value, the default value of the variable is undefined

JavaScript is a weak type. You do not need to specify the type of the variable when declaring it. The type of the variable Decise the statement of

global variables based on the value of the variable: 1: The statement in the function of the function is a global variable, whether there is VAR statement
2: The variable that uses VAR declaration inside the function body is the variable is the variable is the variable that is declared in the function body. Local variables, variables declared without var are global variables

//If you assign a value to a variable type that has not been declared, javascript will automatically use the variable to create a layout variable
For example: a="hello world";
funcation test(){
var c="local variable";//This c is a local variable, and it is also the only way to define a local variable
     b="All variables";//This b is also a total variable
     }
         
Function test2(){
Alert(b);
}
#3: The scope of the variable
The scope of the variable refers to the effective range of the variable in the program
All Variables: variables defined outside all functions, acting on the entire code
Local variables: variables defined within the function body, acting only on the function body

Lesson 6

Application of Operators
1: Assignment operator
Simple assignment operator
For example, var useName='tom';//Simple assignment operator
Compound assignment operator
a+=b;//Equivalent to a =a+b;
a-=b;//equivalent to a=a-b;
a*=b;//equivalent to a=a*b;
a/=b;//equivalent In a=a/b;
a%=b;//equivalent to a=a%b;
a&b=b;//equivalent to a=a&b;logical AND operation
a|=b ;//Equivalent to a=a|b; logical OR operation
a^=b;//Equivalent to a=a^b; logical NOT operator
2: Arithmetic operator
+ - * / %
++ Before ++ add first and then use. After ++ use first and then add.
-- before - before subtracting first and then use. After - before using first and then subtracting.
Note: When performing division operation , 0 cannot be used as a divisor. If 0 is used as a divisor, the keyword infinity

3 will be returned: comparison operator
>greater than =greater than or equal to <=less than or equal to
== is equal to just judgment according to the surface value, and does not involve the data type, alert ("11" == 11); Return True.
===Absolute equals Not only determines the surface value, but also determines whether the data types are the same.
! = is not equal to It is only judged based on the surface value and does not involve the data type.
! == is not absolutely equal. It not only determines the surface value, but also determines whether the data types are the same.

4: Logical operator
! Logical NOT
&&Logical AND. Only when the values ​​of both operands are true, the result will be true
||Logical OR. If only one of the two operands is true, the result is true
5: Conditional operator
The conditional operator is a special ternary operator supported by JavaScript
Grammar format: Operand? Result 1: Result 2;
If the value of the operand is true, the result of the entire expression is result 1
If the value of the operand is false, the result of the entire expression is result 2
6 :String operator
Two ways to connect strings
+. var a="hello"+"world";
+=. var a+="hello world!!!";

Lesson 7
Flow control if, switch statement
1: if conditional judgment statement
1:if(expression){
//Execute the statement inside when expression is true
}
2: If (Expression) {
// Expression is executed in the sentence
} Else {
// Expression is the statement in the execution of the execution
}
3:if(expression){
//Execute the statement inside when expression is true
}else if(expression1){
# }else if(expression2){
                                                                                                                                        use using ’ ’ s ’       use using ’ using ’s ’ using ’s ’ using ’s using ’s ‐ to ‐execute the statement
                                                          ​# //Specify else when neither is satisfied
}
2: switch statement
Advantages: good readability, easy to read
Syntax format
switch(expression){
case condition 1: statement 1;
break;
case condition 2: statement 2;
break;
case condition 3: statement 3;
break;
case condition 4: Statement 4 ; #For, while, do-while statement of process control
1: for loop statement
Syntax format
for(1 initial condition; 2 loop condition; 4 growth step){
3 statement body ;
}
//Execute the initial condition first, and then determine whether the loop condition returns true,
//If it returns false, terminate the condition. If it is true, execute the statement body,
//Then execute Growth pace
//1->2true->3->4->2true->3->4
//1->2false->3-> 4->2false End of for loop
Example
var sum=0;
for(var i=0;i<10;i++){
Sum+=i;
}
alert(sum);
2: while loop statement
Syntax format
while(expression1){
2 statement body;
}
1true->2-> ;1true->2.....
      Example
    var sum=0;
    var i=1;
                                                                    ’ s ’ s ’ ’ s   ’ ‐     ‐ ‐ 1 true‐>2..... #               i++;
        }
          alert(i);
Syntax format
do{
1Execution loop body
}while(2 judgment condition);
1->2true->1->2true.....

Note: The while loop first determines whether the condition is established, and then based on the result of the judgment
Whether to execute the loop body
The do-while loop executes the loop body once first, and then determines whether the condition is true.
So the do-while loop can be guaranteed to be executed at least once.

Example
var sum=0;
var i=1;
do{
}sum+=i;
}while(i<=10);
alert(sum);

The above is the detailed content of JavaScript introductory study guide for beginners. For more information, please follow other related articles on the PHP Chinese website!

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