1 variable name
The variable name must start with a letter and can only contain letters, numbers, and/or underscores, and cannot conflict with reserved words. Variable names are case sensitive.
2. Data Type
2.1 numeric type
The bottom layer of JavaScript is not strictly divided into integers and floating-point numbers. Various numeric types in the underlying JavaScript are processed as floating point numbers, and the two can be freely converted in the program.
2.2 Boolean
Boolean has only two optional values: true and false
2.3 string
A string is zero or multiple characters defined by single or double quotation marks. Javascript also provides escape characters, such as "/F", "/N", line feed, "/R", and "/T, "//" indicates the backslash.
2.4 null
Null indicates no value.
2.5 uncertain value
An uncertain value is used to indicate a variable that has been created but has not been created. For numeric values, the undefined value is Nan; for string values, the undefined value is undefined; For Boolean variables, the undefined value is false.
3. Data Conversion
3.1 convert a string to a numeric value
Javascript provides two built-in functions, parseint and parsefloat, to convert the corresponding string to a value.
3.2 convert a value to a string
The simplest method is to add a control string to the value to convert the value into a string.
4string object
4.1 common methods
String. indexof (string, position) -- From the position (optional) position, search for the position where the first string appears and return.
String. lastindexof (string, position) -- From the position (optional) position, search for the position of the last string in the string and return it.
String. charat (POS) -- returns the character whose position is POs in the string.
String. charcodeat (POS) -- returns the character code whose position is POs in the string. For ASCII characters, the ASCII code is returned.
String. Slice (START, end) -- returns the string whose actual position is start and end.
String. Split (string, limit) -- returns multiple strings whose length does not exceed limit as a separator.
Stringsubstr (START, length) -- returns the substring with the start position and length in the string.
String. substring (START, end) -- returns the substring in the string with the actual position of start and end (including end.
String. Length -- calculate the string length.
String. tolowercase () -- returns the uppercase letters of the original string.
String. touppercase () -- returns the lowercase letters of the original string.
5. Comprehensive Application
Show current date
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> current date </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>
<Script language = "JavaScript">
Function Showtime ()
{
Mytime = new date ();
VaR montharray = new array ", "October", "November", "December ");
VaR dayarray = new array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday ");
Year = mytime. getyear ();
Date = mytime. getdate ();
Hours = mytime. gethours ();
Minutes = mytime. getminutes ();
Seconds = mytime. getseconds ();
SUF = "am ";
If (hours> 12)
{
SUF = "PM ";
Hours = hours-12;
}
If (hours = 0)
Hours = 12;
If (minutes <= 9)
Minutes = "0" + minutes;
If (seconds <= 9)
Seconds = "0" + seconds;
Thetime = "<font seize = 2> today is: </font> <br> <font size = 4>" + year + "year" + montharray [mytime. getmonth ()] + date + "day" + "& nbsp;" + dayarray [mytime. getday ()] + "</font> <br> <font size = 2> current time: </font> "+" <br> <font size = 4 face = Arial> "+ hours +": "+ minutes +": "+ seconds +" & nbsp; & nbsp; "+ SUF +" </font> ";
DT. innerhtml = thetime;
SetTimeout ("Showtime ()", 1000 );
}
</SCRIPT>
<Body onload = "Showtime ()" bgcolor = "5660180" link = "# c0c0c0" vlink = "# c0c0c0" alink = "#008080" text = "# c0c0c0">
<Span id = DT style = "position: absolute; left = 35px; Top = 15px"> </span>
</Body>
</Html>
6 basic statements
In fact, the syntax is similar to that of Java and other advanced languages. The following are some instances.
Use conditional statements for gender selection
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> gender selection </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>
<SCRIPT>
Function showsex ()
{
Onoroff = Document. Forms [0]. Sex [0]. Checked
Statur = (onoroff )? "Handsome guy": "Beautiful Girl"
Alert ("hello" + statur)
}
</SCRIPT>
Enter your gender:
<Form onclick = showsex ()>
<Input type = radio name = sex> male
<Input type = radio name = sex> female
</Form>
<Body>
</Body>
</Html>
For in Loop
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> for in </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>
<Body>
<Script language = "JavaScript">
VaR myobject = new object ();
Myobject. sit0 = "0 ";
Myobject. sit1 = "1 ";
Myobject. sit2 = "2 ";
For (prop in myobject)
{
Document. Write ("attribute" + Prop + "is" + myobject [prop]);
Document. Write ("<br> ");
}
</SCRIPT>
</Body>
</Html>
Calculator
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> calculator </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>
<Script language = "JavaScript">
Function compute ()
{
VaR num1, num2;
Num1 = parsefloat (document. biaodan. shu1.value );
Num2 = parsefloat (document. biaodan. shu2.value );
Num3 = Document. biaodan. jieguo;
OP = Document. all.;
If (OP [0]. Checked)
Num3.value = num1 + num2;
Else if (OP [1]. Checked)
Num3.value = num1-num2;
Else if (OP [2]. Checked)
Num3.value = num1 * num2;
Else if (OP [3]. Checked)
Num3.value = num1/num2;
Else alert ("Enter the operator! ");
}
</SCRIPT>
<Body>
<Form method = "Post" name = "biaodan" id = "diaodan">
<H3 align = "center"> <font color = "#666666" size = "5"> calculator </font> <Div align = "center">
<Table width = "38%" border = "0">
<Tr>
<TD width = "29%"> <Div align = "right"> Number 1: </div> </TD>
<TD width = "71%"> <input name = "Shu1" type = "text" id = "Shu1"> </TD>
</Tr>
<Tr>
<TD> <Div align = "right"> OPERATOR: </div> </TD>
<TD> <input type = "radio" name = "A" value = "0">
+ <Input type = "radio" name = "A" value = "1">
-<Input type = "radio" name = "A" value = "2">
* <Input type = "radio" name = "A" value = "3">
/</TD>
</Tr>
<Tr>
<TD> <Div align = "right"> Number 2: </div> </TD>
<TD> <input name = "shu2" type = "text" id = "shu2"> </TD>
</Tr>
<Tr>
<TD> <Div align = "right"> result: </div> </TD>
<TD> <input name = "jieguo" type = "text" id = "jieguo"> </TD>
</Tr>
<Tr>
<TD> <Div align = "right"> </div> </TD>
<TD> <input name = "Suan" type = "button" value = "computing" onclick = "compute ()"> </TD>
</Tr>
</Table>
</Div>
<Div align = "center"> </div>
<P align = "center"> <font color = "#666666" size = "5"> </font> </P>
<P> </P>
</Form>
</Body>
</Html>
Maximum Value
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> maximum value </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>
<Form name = "biaodan">
Number 1:
<Input type = "text" name = "num1">
<Br>
Number 2:
<Input type = "text" name = "num2">
<Input type = "button" value = "Compare" onclick = "compare ()">
</Form>
<SCRIPT>
Function compare ()
{
VaR;
A = parsefloat (document. biaodan. num1.value );
VaR B;
B = parsefloat (document. biaodan. num2.value );
If (A> B)
Window. Alert ("maximum value:" + );
Else
Window. Alert ("maximum value:" + B );
}
</SCRIPT>
<Body>
</Body>
</Html>