Javascript (ii)

Source: Internet
Author: User

Javascript-recognize statements and symbols

JavaScript 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.

Javascript-notes are important

The 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 comment 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 using/* Comment content */");
/* Multiline comments develop a good habit of writing notes * *
</script>

javascript-What is a variable

What 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.

javascript-Judgment Statement (If...else)

The 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 (condition)
{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 = 18;

if (myage>=18)//myage>=18 is the judging condition

{document.write ("You are an adult.") ");}
else//Otherwise age less than 18

{document.write ("Under 18, you are not an adult.") ");}

</script>

javascript-what is a function

A 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 () {Functional 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 (ii)

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.