Javascript basics 2 data types, statements, and functions

Source: Internet
Author: User

Some browsers in the old version do not support javascript scripts. When a script node is used as a normal content output, sometimes to make the version compatible, comments will be written in the content of the script node. In this way, even though the program fails in the old version, our code will not appear, similar to this (ignore the brackets and the content in the brackets): Copy codeThe Code is as follows: <script type = "text/javascript">
<! -- (Note: line feed is required)
Xxxxxx;
--> (This row cannot be written .)
</Script>

Note: after testing, it is found that js statements cannot be written in the same line as the annotation symbol, otherwise they will become invalid in the new browser.

Javascript data type:

Value Type: includes all numbers.
String type string is enclosed by "" or'
Boolean = true or false.

Variable: the container used to store data. The value stored in the program can change.

Variable Declaration: var variable name [= value];

If the variable is declared inside the function, it is local. If it is outside the function, it is global.

That is to say, no matter which type of variables are declared, var is used. The specific data type is determined after initialization. If it is not initialized, it is an undefined type variable.

We will use the small project mentioned in the previous article js to play with its numerical type.

Write in main.html:Copy codeThe Code is as follows: <Head>
<Title> O. O </title>
<Script type = "text/javascript" src = "js/output. js"> </script>
<Script type = "text/javascript">
Var a = 456;
Var B = "Hello ~ ";
Var c = true;
Var d;
Document. write (a + "" + typeof a + "<br/> ");
Document. write (B + "" + typeof B + "<br/> ");
Document. write (c + "" + typeof c + "<br/> ");
Document. write (d + "" + typeof d + "<br/> ");

</Script>
</Head>

<Body>
This is my JSP page. <br/>
</Body>
</Html>

Then we can see the output:

As you can see, after the value is assigned, var has its own type, but var d is undefined.

Here we can see that the reserved word typeof can return the name of the basic data type. In addition to the three types, the object type can also be returned.

However, for a special type, instanceof is required to return the type name.

For example, if you define a var obj = new Object (); or var date = new Date (); Use typeof to return its type (typeof obj), the output is the object

Data type conversion:

Convert from String to int/float using the function: parseInt (String)/parseFloat (String) for example: parseFloat ("3.14159 ");

Convert from numeric type to string, use toString () function: for example:

Var iNum = 30; 30. toString (2); // Add 2 to the brackets to convert the string to a binary string.

// There can also be octal and hexadecimal. If there is no number, it is a direct conversion.

The next step is to make a small experiment and replace the content under the script node in the file just now:Copy codeThe Code is as follows: <script type = "text/javascript">
Var a = 456;
Var B = "789 ";
Var c = a. toString ()
Var d = parseInt (B );
Document. write (a + "" + typeof a + "<br/> ");
Document. write (c + "" + typeof c + "<br/> ");
Document. write (B + "" + typeof B + "<br/> ");
Document. write (d + "" + typeof d + "<br/> ");
</Script>

Then, run it ~

It seems that the conversion type is actually very simple ~
Operation:
In fact, operations are the same as those in c java. They all involve addition and subtraction multiplier, and or. In fact, there is no difference.
Here we reiterate the difference between I ++ and I.
I ++ executes I + 1 once, but returns I, for exampleCopy codeCode: var I = 50;
If (I ++ <= 50)
Document. write (I );

The running sequence is actually: Value assignment: I = 50, judgment: if (I <= 50), execution I = I + 1, and execution of the judgment statement: xxxx. So the final output is 51, so I won't be able.
If ++ I is used, the returned value is the value after 1, that is, there will be no output.

--------------------------------------------------------------------------------
Statement section
Condition Statement: if and switch
In fact, it is the same as c. This part does not need to be mentioned more, that is, the condition statement should also pay attention to a little detail:
In the if Statement of js, false is returned in the following cases: null \ undefined \ null String "" \ 0 \ false
For empty strings, note that var s = "" and var s = new String ("") are different. The latter opens up the memory space and returns true.
Let's verify it ~, Under a javascript node:Copy codeThe Code is as follows: <script type = "text/javascript">
Var;
Var B = new String ("");
If (null) document. write ("null ");
If (0) document. write ("0 ");
If (a) document. write ("a undefined ");
If (false) document. write ("false ");
If ("") document. write ("empty string ");
If (B) document. write ("new String ()");
Document. write ("<br/> ");
</Script>

Save and refresh the main page:

Oh (⊙ o ⊙), it is true that only new string appears ~.

The swith statement is also good:Copy codeThe Code is as follows: <script type = "text/javascript">
Var date = new Date ();
Var day = date. getDay ();
Document. write (day + "<br/> ")
Switch (day ){
Case 6, 7: document. write ("weekend \ (^ o ^ )/");
Break;
Case 5: document. write ("hold on ~! Last day! ");
Break;
Default: document. write ("work days .");
}
Document. write ("<br/> ");
</Script>

View output:

OK. The following is a loop statement: while do-while for-in
The only difference between the first two is that the order of loop and judgment is different. do-while is more than while, and I will not give an example.
For loop I believe everyone is familiar with it, so let's look at the for-in sentence.
This is actually for arrays, And the array initialization in js is also quite strange, for example, we write in the script node :( also pay attention to the array initialization, using brackets)Copy codeThe Code is as follows: <script type = "text/javascript">
<! --
Document. write ("test <br/> ");
Var a = [3, 4, 5, 7];
For (var test in ){
Document. write (test + ":" + a [test] + "<br/> ");
}
-->
</Script>

View the output:

From this result, we will discuss the following points:
Test is actually an int number to represent the number mark of the array.
For-in can only take one number for each loop, which is generally used for exhaustion.
In some cases, you can only use for-in for reference. for example, the content stored in an array contains strings and numbers.
(Of course, if it is hard to say, it is not only for-in, but for-in is much more convenient)
In fact, the first article on functions is simple.
1. No return value is required before the function name, and no type is required in the parameter list.
2. the variables defined in the function are local variables and cannot be called outside.
The format is basically as follows:
Function Name (parameter list ){
Xxxxxxx;
[Return xxxx;] // optional
}
OK. Now that you know the format, let's try:

Copy codeThe Code is as follows: <script type = "text/javascript">
<! --
Function add (a, B ){
Var c = a + B;
Return c;
}
Var x = 20, y = 45;
Document. write (add (x, y ));
Document. write ("<br/>" + c );
-->
</Script>

The output only contains numbers. That is to say, the second line directly ignores the numbers. Even the undefined standards are not met. Because it is a local variable. After the function is executed, it is discarded.

In addition to the standard writing, there is also a rebellion, that is, when defining a function, no parameters are written, but parameters can also be used in the function body. In this way, when there is a parameter, there can be output. In this case, because the parameters used do not have a name, they are all stored in the arguments array. For example:

Copy codeThe Code is as follows: <script LANGUAGE = "JavaScript">
<! --
Function show (){
For (var temp in arguments)
Document. write (arguments [temp] + "");
}
Show (3, 4, "Dumpling", 4.67 );
-->
</Script>

PS: I changed the attributes in the script node. In fact, javascript can also be declared. The LANGUAGE must be in uppercase.

View output:

Note: it is also possible to perform operations using the data stored in arguments in the tested function, for example

Copy codeThe Code is as follows: <script LANGUAGE = "JavaScript">
<! --
Function add (){
Return arguments [0] + arguments [1];
}
Document. write (add (4, 44) + "");
Document. write (add (4, "e55 "));
-->
</Script>

The output is 48 4e55. Of course, because two parameters are used in the function body, if you only give one parameter when calling the function, the output result is not only one value. If you are interested in testing it yourself ~ If you call three parameters, the third parameter will be ignored relentlessly.
In addition to the regular definition above, there are also some other methods to define functions, which are relatively non-mainstream. I don't like to use them, but I still need to write them out to understand them:
One is: var add = new Function ("parameter", "parameter",..., "Function body ");Copy codeThe Code is as follows: <script LANGUAGE = "JavaScript">
<! --
Var add = new Function ("a", "B", "var c; c = a + B; return c ");
Document. write (add (4, 87 ));
-->
</Script>

The output is correct. As you can see, the last sentence does not require any extra points. If there is a problem, the truth here is that Function is actually a class, then add becomes the name of the function.
There is also a way to write:Copy codeThe Code is as follows: <script LANGUAGE = "JavaScript">
<! --
Var show = function (name ){
Document. write ("<br/>" + "Hellp" + name );
}
Show ("Dumpling ");
-->
</Script>

Output what everyone understands .. The writing method is to write the function name to the front.
Since it has been proved that the function is actually an object, of course it also has some function functions that can be called, such as toString () or valueOf () functions can be complete, length can return the number of parameters of the function.
Let's try:Copy codeThe Code is as follows: <script LANGUAGE = "JavaScript">
<! --
Var show = function (name ){
Document. write ("Hello" + name );
}
Show ("Dumpling ");
Document. write ("<br/>" + show. valueOf ());
Document. write ("<br/> number of arguments:" + show. length );
-->
</Script>

Output:
Hello Dumpling
Function (name) {document. write ("Hello" + name );}
Number of arguments: 1
OK. Here is the second article ~ Continue tomorrow (/^ o ^ )/

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.