Home  >  Article  >  Web Front-end  >  JavaScript introductory study guide for beginners

JavaScript introductory study guide for beginners

伊谢尔伦
伊谢尔伦Original
2017-06-16 10:05:512183browse

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
9cc1e8e6d40187c6e71a342c870da16d

javascript program
2cacc6d41bbb37262a98f745aa00fbf0
javascript can be inserted in the middle of the 93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1 tag,
can also be placed in the 9bbcacf754ba6508fcaf29af414e244c in the middle of tags
Most commonly placed between 93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1 tags

The case is as follows, insert the javascript code in 93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1 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>

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>

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

c84ddcd03f7f3452cc17708b2c97fe5d
2cacc6d41bbb37262a98f745aa00fbf0
(Recommendation) It is generally better to write the above code in the middle of 93f0f5c25f18dab9d176bd4f6de5d30e9c3bca370b5104690d9ef395f2c5f8d1

In the file with the .js suffix , there is no need to use 3f1c4e4b6b16bbbd69b2ee476dc4f83a2cacc6d41bbb37262a98f745aa00fbf0 tags to enclose

77f7724499889f2edf8d98094d11b671 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>

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);
    
}

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 8ab4dff2fdaade4be21b12a1c15e7e63=greater than or equal to 592af17feecb883955de8b46da9d30482true->3->4->2true->3->4
//1->2false->3-> 4->2false End of for loop
Example
var sum=0;
for(var i=0;i06ee6057da54f7fb1e422c6a8e78b9602-> ;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!

Statement:
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