Basic JS syntax

Source: Internet
Author: User

Javascript runtime environment and code location

Writing JavaScript scripts does not require any special software. A text editor and a web browser are enough. JavaScript code runs in a web browser.
The code written in Javascript must be embedded in an HTML document for execution. This can be achieved through two methods. The first is to directly write the JavaScript code in an HTML file, this method is applicable only to JS programs on one page. The other is to store JavaScript code into an independent file (. JS extension.

Embed Javascript directly into a page file

<% @ Page contenttype = "text/html; charset = UTF-8" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> welcome to "my transaction memos" </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<SCRIPT src = "Web/JS/strutil. js" type = "text/JavaScript"> </SCRIPT>
</Head>

<Body>
<Div> This page should soon disappear. If it is stopped, the Web container is stopped, or the JavaScript function is not enabled.
<Form method = post action = "showpage? Page = login ">
</Form>
<Div>
</Body>
</Html>

<Script language = "JavaScript">
<! --
Document. Body. onload = function (){
Document. Forms [0]. Submit ();
}
// -->
</SCRIPT>

Store Javascript into a separate file (page file)

<% @ Page contenttype = "text/html; charset = UTF-8" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> "My transaction memos" User Logon page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<SCRIPT src = "Web/JS/ajax. js" type = "text/JavaScript"> </SCRIPT>
<LINK rel = "stylesheet" REV = "stylesheet" href = "Web/CSS/style.css"
Type = "text/CSS"/>
</Head>

<Body>
<Div id = "branding"> welcome to the "personal transaction memorandum". Enter your username and password, and then press the logon key to log on.
<Form method = post action = "showpage? Page = logincheck ">
<Table bgcolor = "# ffffff" id = "tbsort" class = "listing" width = "200" align = center>
<Tbody id = "logintable">
<Tr> <TH align = "center" colspan = 3> User Logon. </Th> </tr>
<Tr>
<TD width = 50> User name: </TD>
<TD width = 150> <input type = "text" name = "username" value = ""
Style = "width: 300px; Height: 20px"/> </TD>
</Tr>
<Tr>
<TD width = 50> password: </TD>
<TD width = 150> <input type = "text" name = "userpswd" value = ""
Style = "width: 300px; Height: 20px"/> </TD>
</Tr>
<Tr>
<TD width = 50> </TD>
<TD width = 150> <input type = "Submit" value = "Logon"
Style = "width: 100px; Height: 20px"/> </TD>
</Tr>
</Tbody>
</Table>
</Form>
<Div>
</Body>
</Html>

Store Javascript into a separate file (Ajax. JS)

VaR prjname = "/mytodoes /";
VaR ajaxobj;

Function createajaxobject (){
Try {return New activexobject ("msxml2.xmlhttp");} catch (e ){};
Try {return New activexobject ("Microsoft. XMLHTTP") ;}catch (e ){};
Try {return New XMLHttpRequest ();} catch (e ){};
Alert ("XMLHttpRequest not supported! ");
Return NULL;
}

Function $ (ID ){
Return document. getelementbyid (ID );
}

Statements and comments in Javascript

The statements in JavaScript are the same as those in Java. A semicolon ';' is added at the end of a line. Although multiple statements can be written in Javascript in one line, this is not recommended.
The annotation in Javascript is also the same as in Java, and a single line is annotated ,/*.... */Comments multiple lines, although HTML-style comments <! -- ***** --> It is also valid in JS, but it is not recommended to do so.

Javascript Variables

In JS, variables allow letters, numbers, dollar signs, and underline characters. variable definitions use the VaR keyword, as shown in
VaR age;
Age = 23;
VaR name = "Andy ";
Although JavaScript allows programmers to assign values to variables without declaring them in advance, we strongly recommend that you do not.
In JS, variables and other syntax elements are case-sensitive, such as age, age, and age. They are not the same variable.

Javascript is a weak language

Unlike the stronugly typed programming language, such as Java and C #, which forces programmers to declare data types, JS does not require programmers to describe the types, this is the so-called weak type "weakly typed" language. this means that programmers can change the data type of a variable at will.
The following statements are absolutely not allowed in Java, but there is no problem in JS:
VaR age = 23;
Age = "Twenty three"
JS does not care whether the value of age is a string or a variable.

Data Type in Javascript-string

The string must be placed in single or double quotation marks. For example
VaR name = "Andy ";
VaR name = 'bill ';
Double quotation marks are recommended in General cases. If a string contains double quotation marks, the string should be placed in single quotation marks. Otherwise, the string should be placed in double quotation marks.

Data Type in Javascript-Value

JS does not have the difference between int, float, double, and long. It allows programmers to use decimal places and Integers of any digits. In fact, the value in JS should be called a floating point number.
For example:
VaR salary = 10000;
VaR price = 10.1;
VaR temperature =-6;

Data Type in Javascript-Boolean Value

The Boolean value in JS is consistent with that in Java. True indicates true, and false indicates false, for example:
VaR ismale = true;
VaR isfemale = false;

Note that the Boolean values true and false are not written as strings "true" and "false '.

Functions in JS

If you need to use the same group of statements multiple times, you can package these statements into a function. A function is a set of statements that allow people to call in code at any time. In effect, each function is equivalent to a short script.
Unlike every function in Java, a function in JS does not need to belong to a class. In usage, it is similar to a static public function in Java, you can use the file where the function is introduced.

Function syntax in JS

In JS, the syntax of a function is as follows:
Function fname (ARGs ){
Statements;
}
Function is a fixed sign of a function; fname is a function name; ARGs is a function parameter, which can have many, as long as you separate them with commas; statements is the statement in it, the end of each sentence is the same as that in Java.

In the script (PAGE) that defines this function, you can call this function from any location. After introducing this page, you can also access it from other pages.

Generally, functions that are called on multiple pages are suitable for a wide range of applications. We usually place them on a JS page and then introduce them to pages that require these functions; for a function that only applies to one page, it is better to put it in a single page.

JS function return value

In JS, a function not only accepts data in the form of parameters, but also runs the code. Like functions in other programming languages, it can return data.
To allow the JS function to return data, you do not need to do anything on the function signature. You only need to use the return statement to return the number you want to return. For example:
Function substract (OP1, OP2 ){
Return op1-op2 ;}
}

Scope of variables in JS

In JS, we advocate the use of VaR to define a variable. Any variable has a scope problem. According to the definition method and location, it may be global, it may also be local.
VaR is defined in the script file. It does not belong to any function variable, and its scope is global. It can be referenced anywhere in the script, including the internal function. The scope of global variables is the entire script.
When VaR is used to define a variable in a function, its scope is local. Its scope is limited to this function and cannot be used outside the function.
Variable defined in the function without var. Its scope is global. If a variable with the same name already exists in your script, the function will overwrite the value of the existing variable.
When defining a function, we must explicitly declare all its internal variables as local variables. If we never forget to use the VaR keyword in the function, you can avoid any form of ambiguity risks.

Array in JS

In JS, we use the array keyword to declare an array and define the length of the array during declaration, for example:
VaR arr = array (3 );
The array length is known only when running. In JS, we can also define the array as follows:
VaR arr = array ();
When adding an element to an array, you need to give the value of the new element. You also need to specify the storage location for the new element in the array. This location is given by the subscript, for example, arr [1] = 4.

Example of defining arrays in JS

Definition Method 1:
VaR arr = array (3 );
Arr [0] = "Liu Bei"; arr [1] = "about"; arr [2] = "Zhang Fei ";
Method 2:
VaR arr = array ();
Arr [0] = 3; arr [1] = 4; arr [2] = 5;
Method 3:
VaR arr = array ("1", 2, true );
Definition method 4:
VaR arr = ["Zhengdong", "Pingxi", "Zhennan", "sweeping north"];

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.