Chapter One: JavaScript Basics---(i) basic grammar __java

Source: Internet
Author: User
Tags logical operators

Very eager to learn to complete the project quickly, do not want to spend too much time to learn a language, although this article does not detail the features but the following

Code is sufficient to understand the JavaScript syntax, observe the following code, and copy it to the text and open it in the browser.

JavaScript is case sensitive, here we quickly understand its syntax

As an introductory exercise you can tap each code yourself.


1:helloworld, we wrote the first example

<script language= "JavaScript" >
    document.write ("Hello world!!!");
</script>

2: Dialog box form display HelloWorld three kinds of forms

<a href= "Javascript:alert (' Hello world!!! ')" > Please click </a>

<a href= "onclick=" alert (' Hello world!!! ') " > Please click </a>

3:with keyword Usage--simplifies code with
An open field statement that represents the owning object of any expression in the WITH statement, or the function of a class that is immediately followed by the with
: A default object is established for a program. Simplified code <br> 
<br> format:
<br>with (< object >) {
<br> < statement group >
<BR>}

<Script> with
(document) {
    write ("<br><Li> snapped up items:");
    Write ("<li>viewsonic 17\" monitor.) ");
    Write ("<li>epson printer. ");
}
document.write ("<br>---------------");
document.write ("<Li> snapped up Goods:");
document.write ("<li>viewsonic 17\" monitor.) ");
document.write ("<li>epson printer. ");
</Script>
4: Variable declaration, always beginning with Var

Representation of <BR>JAVASCRIPT Data:

<br> integer: By positive and negative numbers, number composition, octal, decimal, hexadecimal; <br> floating point number: by positive and negative numbers, numbers and decimal points, conventional notation, scientific notation; <br> logical value: True, False <br> String values: Single quotes, double quotes <br> null values: null <br> undefined values: objects that do not exist, defined but not assigned; <br> <br> <script language= " 
JavaScript "> var stdid; 
var total=3721; 
var notnull=true; 

var name= "Bruce Lee", sex= "Mr.";  

var i=j=0;
var hobby=["Listening to music", "watching movie"];
Hobby[2]= "123";
var test;
stdID = 2004007;
document.write ("Variable stdid=" +stdid+ "<br>");
document.write ("Variable i=" +i+ "<br>");
document.write ("Variable j=" +j+ "<br>");
document.write ("Variable name=" +name+ "<br>");
document.write ("Variable sex=" +sex+ "<br>");

document.write ("Variable notnull=" +notnull+ "<br>");
document.write ("Variable hobby=" +hobby+ "<br>");
document.write ("Variable hobby[0]=" +hobby[0]+ "<br>");
document.write ("Variable hobby[1]=" +hobby[1]+ "<br>");
document.write ("Variable hobby[2]=" +hobby[2]+ "<br>");
document.write ("Variable hobby[3]=" +hobby[3]+ "<br>");
document.write ("Unassigned variable test=" +test+ "<br>"); document.write ("A value that does not exist" (0).0/0.0) = "+ (0.0/0.0) +" <br> "); var t1=1.7976931348623157e+10308 document.write ("positive number out of range value" +t1) document.write ("<br/>") var t2=-  1.7976931348623157E+10308 document.write ("negative number out of range value" +t2) </script>

5: Global variable Declaration

<Script>
var langjs = "JavaScript";//langjs is the global variable
test (); 
function test () {
    var langvbs = "VBScript";//langvbs is a local variable
    document.write ("<LI>" + langjs);
    document.write ("<LI>" + langvbs); 
}
document.write ("<LI>" + langjs);
document.write ("<LI>" + langvbs);

</Script>
6: Operational symbols and subtraction
<script language= "JavaScript" >
var x = one; 
var y = 5;

With (document) {
    write ("x = one, y = 5");
    Write ("<li>x + y is", x + y);
    Write ("<li>x-y is", X-y);
    Write ("<li>x * y is", x * y);
    Write ("<li>x/y is", x/y);
    Write ("<li>x% y is", x% y);
    Write ("<li>++ x is", + + x);
    Write ("<li>--y is",--y);
    Write ("<li>x is", x);
    Write ("<li>y is", y);
    Write ("<li>x--is", x--);
    Write ("<li>y--is", y--);
}
</script>

7:++ operations

<Script>
var x = y = 3; 


With (document) {
    write ("y++=", (y++), "<br>");
    Write ("++y=", (++y), "<br>");
    Write ("x = 3, y = 5 <br>");
    Write ("If x = after y++ operation:");
    x = y++;//y→x,y+1→y
    Write ("x is", X, "; Y is ", Y, <br>");
    Write ("Again x = ++y operation:");
    x = ++y;//y+1→x,y+1→y
    Write ("x is", X, "; Y is ", y);
}
</Script>
8: Logical Operators and
&& | | 

!
Example 1: <Script> var t = true;

var f = false;
With (document) {Write (Result of "<ol><li>true && true", T && t);
Write (the result of "<li>true && false", T && F);
Write (Result of "<li>false && true", F && t);
Write ("<li>false && false result is", F && f);
The result of write ("<li>true && (1==1)", T && (1==1));
The result of write ("<li>false && ' A" is ", f && ' a ');
The result of write ("<LI> ' a ' && false", ' a ' && f);
The result of write ("<li>true && ' A" is ", T && ' a ');
The result of write ("<LI> ' a ' && true", ' a ' && t);
The result of write ("<LI> ' a ' && ' B ') is", ' a ' && ' B ');
Write ("<li>1 && 1 results are", 1 && 1);
Write ("<li>1 && 0 results are", 1 && 0);
Write ("<li>true && 0 results are", true && 0);
Write ("<li>true && 1 results are", true && 1); Write ("&LT;LI&GThe result of the T;true && ' 0 is ', true && ' 0 '); &&amp: One is not a logical value, as long as the value of the first operand is flase, the value of the first operand is returned flase, otherwise, the value of the second operand is returned </Script>
9: logical operator or
<Script>
var t = true;
var f = false;

With (document) {
write ("<ol><li>true | | The result of True is ", T | | t);
Write ("<li>true | | The result of false is ", T | | f);
Write ("<li>false | | The result of True is ", F | | t);
Write ("<li>false | | The result of false is ", F | | f);
Write ("<li>true | | The result of (1==1) is ", T | | (1==1));
Write ("<li>false | |" The result of a ' is, f | | ' A ');
Write ("<LI> ' A" | | The result of false is ", ' A ' | | f);
Write ("<li>true | |" The result of a ' is, T | | ' A ');
Write ("<LI> ' A" | | The result of True is ", ' A ' | | t);
Write ("<LI> ' A ' | |" The result of B ' is ', ' a ' | | B ');
Write ("<li>1 | | 1 result is ", 1 | | 1);
Write ("<li>1 | | 0 result is ", 1 | | 0);
Write ("<li>true | | 0 result is ", true | | 0);
Write ("<li>true | | 1 result is ", true | | 1);
Write ("<li>true | |" 0 ' result is ', true | | ' 0 ');
    //|| : One is not a logical value, the value of the first operand is returned as long as the first operand is a value of true, character, or Non-zero, otherwise, the value of the second operand is returned
}
</Script>
10: The logical operator takes the inverse
<Script> with
(document) {
    write ("<li>!true result is",!true);
    Write ("The result of <li>!false is",!false);
    Write ("<li>!" The result of a ' is,! ' A ');
    Write ("The result of <li>!0 is",!0);
    Write ("The result of <li>!1 is",! 1);
    Write ("The result of <li>!2 is",! 2);
    Write ("<li>!" 0 ' The result is ',! ' 0 ');
    Write ("<li>!" 1 ' The result is ',! ' 1 ');
    Write ("The result of <li>!-1 is",!-1);
}
</Script>
11: Logical Operation Priority


The 12:this keyword this usually points to the function we are executing, or to the object to which the function belongs

<Script>
function Validate (obj) {
    alert ("The value you entered is:" + obj.value);
} </Script>
Please enter any character:<br>
<input type= "text" onkeyup= "Validate (This)" >
<BR>
Execute script Long Press when Button is released does not trigger
13: Conditional operator (three-mesh operator)
The <Script>
//forms collection returns a reference to all Form objects in the document.
function Showsex () {
Onoroff = document.forms[0].sex[0].checked
status = (Onoroff)? "Handsome": "Beautiful"
alert ("hello!" + status)
}
</Script> 
Please enter your gender:
<form >
<input type =radio name=sex onclick=showsex () > Boys
<input type=radio name=sex onclick=showsex () > Girls
</form >

14: NOTE statements

<Script>
//single-line Note
  //Multiline comment
          //

//Example:
/***************************************
*             Source Home * * http://www.mycodes.net * *      Download: http://www.mycodes.net      *     Forum Http://www.mycodes.net/bbs    *
*****************************************/
</ Script>
15:if.......else ...
<Script> 
var now = new Date ();
var hour = now.gethours ();

document.write (now);
document.write ("<br>");
if (6 < hour && Hour <)
    document.write ("Now" +hour+ "point is Daylight");
else
    document.write ("Now" +hour+ "point is Night");
</Script>
16:if.......else.....if
<Script> 
var now = new Date ();
var day = Now.getday ();
var dayname;

if (day = = 0) Dayname = "Sunday";
else if (day = = 1) dayname = "Monday";
else if (day = = 2) Dayname = "Tuesday"; 
else if (day = = 3) Dayname = "Wednesday";
else if (day = = 4) Dayname = "Thursday"; 
else if (day = = 5) Dayname = "Friday";
else Dayname = "Saturday"; 

document.write ("Today is Happy", dayname);
</Script>
17:while statement
<Script> 
var i = 5;

while (i > 0) {
    document.write ("i =", I, "<BR>");
    i--;
} 
</Script>
18:do.....while statement
<Script> 
var i = 5;

do {
    document.write ("i =", I, "<BR>");
    i--;
} while (i > 0) 
</Script>

19:for Loop statement
<Script> 
for (var i = 5; i > 0; i--) {
    document.write ("i =", I, "<BR>");
} 
</Script>
20:for in (i) use to traverse all subsets of an array

21:for in (ii) Use to traverse all properties of an object

<br> format:
<br>
<br>for (variable in object) {
<br>    < statement group >
<BR>} 
<br>
<br> Example:
<br>
<Script>
function member (name, sex) {//constructor function
    member this.name = name;
    This.sex = sex;
}

function Showproperty (obj, objstring) {
var str = "";
for (var i in obj)
    str = objstring + "." + i + "=" + Obj[i] + "<BR>";
 return str;
}

Papa = new Member ("Yang Hongwen", "Boy")//Create object instance Papa
document.write (Showproperty (Papa, "Papa")) 
</Script>



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.