The JavaScript code executes when the page reads to the statement.
1<! DOCTYPE html>234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5Location of the <TITLE>JS code </title>6<script type= "Text/javascript" >7document.write ("I Love");8</script>9Ten<body> One<script type= "Text/javascript" > Adocument.write ("You"); -</script> -</body> theOperation Result:
I love You
Vii. Understanding Statements and symbolsJavaScript statements are commands that are sent to the browser. The purpose of these commands is to tell the browser what to do.
Each line of JavaScript code format: 语句;
Let's take a look at the following code
<script type= "Text/javascript" > alert ("hello!"); </script>
In the example alert("hello!");
is a JavaScript statement.
The end of a line is considered the end of the statement, usually with a semicolon at the end ";"
to represent the end of the statement.
Look at this code, there are three statements, each sentence after the end of ";", in order to execute the statement.
<script type= "Text/javascript" > document.write ("I"); document.write ("Love"); document.write ("JavaScript");</script>
Attention:
1. ";" Semicolon to enter in the English state, the same, JS code and symbols are entered in the English state.
2. Although the semicolon ";" can also not write, but we have to develop a good habit of programming, remember to write a semicolon at the end of the statement.
Viii. Annotations are importantThe role of annotations is to improve the readability of your code, to help you and others to read and understand the JavaScript code you write, and the content of the comments will not be displayed on the page. Comments can be divided into single-line comments and multiline comments.
For readability, the comment is generally placed at or around the end of the statement that needs to be interpreted.
A single-line comment, with the symbol "//" before the content of the comment.
<script type= "Text/javascript" > document.write ("single-line annotation using '//'"); I am the comment that the statement function outputs content in a Web page </script>
Multiline comments start with "/*" and End with "*/".
<script type= "Text/javascript" > document.write ("Multi-line Comment use/* Comment content */"); /* Multiline comment develop good habit of writing notes */</script>
Nine, what is a variableWhat is a variable? Literally, a variable is a variable amount; From a programmatic standpoint, a variable is a memory used to store a certain number of values. We can think of variables as a box, in order to distinguish the box, you can use Box1,box2 and other names to represent different boxes, BOX1 is the name of the box (that is, the name of the variable).
Define variables using the keyword var, with the following syntax:
var variable name
Variable names can be named arbitrarily, but follow the naming conventions:
1. The variable must start with a letter, an underscore (_), or a dollar ($) symbol.
2. You can then use any number of English letters, numbers, underscores (_), or dollar ($) characters.
3. You cannot use JavaScript keywords with javascript reserved words.
Variables must be declared and then assigned, as follows:
var mychar;mychar= "JavaScript"; var mynum = 6;
Variables can be assigned repeatedly, as follows:
var mychar;mychar= "javascript"; mychar= "Hello";
Attention:
1. in JS, case-sensitive , such as variable MyChar and MyChar is not the same, the expression is two variables.
2. Variables Although can also not be declared, directly used, but not standardized, need to first declare, after use.
Ten, Judgment statementThe If...else statement executes the code when the specified condition is true and executes the else code when the condition is not established.
Grammar :
if Conditions {Code executed when condition is set} Else {Code executed when the condition is not valid}
Let's say we age to determine whether an adult, such as the age of 18 years old, is an adult, or not an adult. The code represents the following :
<script type= "Text/javascript" > var myage =; if (myage>=18) //myage>=18 is the judging condition {document.write ("You are an adult. ");} else //otherwise age less than {document.write ("Under 18, you are not an adult.") ");} </script>
1<! DOCTYPE html>234<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5<title>if...Else</title>6<script type= "Text/javascript" >7var score = 80;//score Variable Storage score, initial value is8 if(score>=60) 9 {Tendocument.write ("Great, good grades." "); One } A Else - { -document.write ("Come on, don't pass the grade.") "); the } -</script> - -<body> +</body> -Xi. what is a functionA function is a set of statements that complete a particular function. Without a function, it may take five lines, 10 rows, or even more code to complete the task. In this case, we can put the code block that completes the specific function into a function, call this function directly, save the trouble of repeatedly inputting a lot of code.
How do you define a function? The basic syntax is as follows:
function name () {function code;}
Description
1. function defines the keyword for the functions.
2. "Function name" you take the name of the function.
3. "Function code" is replaced with code that completes a specific function.
Let's write a simple function that implements the addition of two numbers and give the function a meaningful name: "ADD2", the code is as follows:
function Add2 () { var sum = 3 + 2; alert (sum);}
Function call:
Once the function is well defined, it cannot be executed automatically, so it needs to be called, just write the function directly where it is needed, and the code is as follows:
JavaScript Introductory article