A preliminary understanding of JavaScript

Source: Internet
Author: User

There are two ways to call into JS (directly in the HTML document to use, calling the external JS document)

1. Use directly in HTML
<body>
<script type= "Text/javascript" >
document.write ("The first JavaScript instance! ")
</script>
<p> the line text does not belong to JavaScript oh,</p>
</body>

2. Call the external

<script src= "apply for points. js" type= "Text/javascript" ></script>

Comments:

Single line://

Multiple lines:/* text */

Attention:

1.JS Case Sensitive

2.JS identifier The first character must be a letter, underscore, or dollar sign ($), other characters can have numbers, but cannot have spaces or other characters

3. The user-defined identifier cannot have the same name as the reserved word, and the identifier must be in the same row when used

4. Ignore extra spaces

5. For special characters such as single quotes, double quotes, ";", and "&", use the slash "\" in the code to differentiate

6. In JS, the semicolon at the end of the code is optional, unless you want to place more than one statement in a row

Variable declaration:

var variable name;

var variable name 1, variable name 2, variable name 3;

var variable name = variable value;

Assignment operators:

X+=y equivalent to X=x+y

X&=y equivalent to X=x&y

Precedence of logical operators:!>&&>| |

Conditional operators:

(conditions)? Value 1: Value 2

For example: "(a>b) a:b" is a conditional expression with the order of execution: if (a>b) satisfies the condition, the value of the condition expression is a, otherwise the value is B

If SELECT statement:

The output is:

Switch SELECT statement:

<!doctype html>
<meta charset= "UTF-8" >
<meta name= "Generator" content= "editplus®" >
<meta name= "Author" content= "" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" >
<title>Document</title>
<script type= "Text/javascript" >
function MyChoice () {
var Selvalue=myform1.myselect.value;
Switch (selvalue) {
Case "reading": Alert ("You have chosen to read as a personal hobby!") "); break;
Case "Weibo": Alret ("You chose Weibo as a personal hobby!") "); break;
Case "Game": Alret ("You chose the game as a personal hobby!") "); break;
Default:alret ("The original three options you are not interested!") ");
}
}
</script>
<body>
<form name= "MyForm1" >
<select name= "Myselect" >
<option value= "reading" > Reading </option>
<option value= "Weibo" > Weibo </option>
<option value= "Games" > Games </option>
<option value= "Other" > Other </option>
</select>
<input type= "button" value= "OK" onclick= "MyChoice ()" >
</form>
</body>

While Loop statement:

<body>
<script type= "Text/javascript" >
var I=1;
while (i<=7) {
document.write ("-" + "Week" +i+ "-");
i++;
}
</script>
</body>

Show results:-week 2--week 3--Week 4--Week 5--Week 6--Week 7-

A preliminary understanding of JavaScript

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.