JavaScript basic article 2 data types, statements, Functions _ Basics

Source: Internet
Author: User
Before writing a piece of nonsense: because the older version some browsers do not support JavaScript script, encountered script node, as the normal content output, so sometimes in order to make version compatible, will be in the script node content to write annotation symbol, so that in the old version although the program will be ineffective, But it doesn't appear to be our code, like this (please ignore the parentheses and the things in parentheses):
Copy Code code as follows:

<script type= "Text/javascript" >
<!--(Note that the line must be wrapped here)
xxxxxx
--> (this line is also not allowed to write anything.) )
</script>

Note: After testing, the JS statement can not be written in the same line as the annotation symbol, otherwise it will be invalidated in the new version of the browser.

Data type of javascript:

Numeric type: All numbers are included.
String type string is enclosed in "or" to represent
Boolean type Boolean =true or False.

Variables: Containers that are used to store data that can be changed by the values stored in the program.

Variable declaration: var variable name [= value];

If the variable is declared inside the function, then it is local, and if it is outside the function, it is global, meaning you understand.

That is, regardless of which type of variable is declared, Var is used, and exactly what data type is determined after initialization. If it is not initialized, then it is a variable of undefined type.

Let's use the small project mentioned in the previous JS to play with its numeric type.

Write in main.html:
Copy Code code as follows:

<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 + "<br/>");
document.write (b + "" + typeof B + "<br/>");
document.write (c+ "" + typeof c+ "<br/>");
document.write (d+ "" + typeof d+ "<br/>");

</script>

<body>
This is the My JSP page. <br/>
</body>

And then we look at the output:

As you can see, when you assign a value, VAR has its own type, but the undefined var d is undefined.

Here we can see, typeof this reserved word, you can return the name of the base data type, in addition to these three kinds, can return the type of object.

But if it's a special type, you need instanceof 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), which outputs an object

Conversion of data types:

Convert from String to int/float use functions: parseint (String)/parsefloat (string) For example: parsefloat ("3.14159");

Converts from a numeric type to a string, using the ToString () function: for example:

var inum=30; 30.toString (2);//After parenthesis, add a 2, meaning to convert to binary string.

There can also be 8 and hexadecimal, if there is no number, it is directly converted.

The next step is to do a small experiment, to the file in the script node under the content Exchange:
Copy Code code as follows:

<script type= "Text/javascript" >
var a=456;
var b= "789";
var c=a.tostring ()
var d=parseint (b);
document.write (A + "" + typeof + "<br/>");
document.write (c+ "" + typeof c+ "<br/>");
document.write (b + "" + typeof B + "<br/>");
document.write (d+ "" + typeof d+ "<br/>");
</script>

Then, run it.

It looks like the conversion type is really simple.
Operation:
In fact, the same as C Java, are added and minus the multiplier ah and ah, or AH, in fact, no difference.
Here is a i++ of the difference between the ++i.
i++ is performed once i+1, but returns I, for example, I write a sentence
Copy Code code as follows:

var i=50;
if (i++<=50)
document.write (i);

Then the order of the runtime is actually: assignment: i=50, Judgment: if (i<=50), executing i=i+1, executing the sentence after judgment: xxxx. So the final output is 51, I don't have screenshots.
If there is a ++i, then the value returned is 1, which means there is no output.

--------------------------------------------------------------------------------
Statement section
Conditional statements: if and switch
In fact, and C is the same, this part does not need to say more, is conditional statement also pay attention to a little detail:
JS if statement, return to false:null \ undefined \ null string "" \ 0 \ False in the following cases
Also note in relation to the empty string: var s= "" and var s=new string ("") are not the same, which open up memory space, so return true.
Now let's check it out. Under ~,javascript node:
Copy Code code as follows:

<script type= "Text/javascript" >
var A;
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>

Then save and refresh the main page:

Alas (⊙o⊙), sure enough, only the new string appeared ~.

Swith statement also try it well:
Copy Code code 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, Next is the Loop statement: while Do-while for-in for
The first two only difference is that the loop and the order of the judgments are different, do-while than while more than one cycle, I do not give an example.
For loop I believe that we can not be ripe, we will see for-in this sentence.
This is actually for the array, JS array initialization is also very strange, such as we write in the script node: (also note the initialization of the array, with the brackets)
Copy Code code as follows:

<script type= "Text/javascript" >
<!--
document.write ("test<br/>");
var a=[3,4,5,7];
for (var test in a) {
document.write (test+ ":" +a[test]+ "<br/>");
}
-->
</script>

We look at the output:

From this result we explore a few points:
Where test is actually a number of int to represent the number of arrays.
For-in each cycle can only walk a few, generally used to poor lift.
In some cases, you can only use for-in to make a poor move, such as the contents of the array stored in string, there are numbers.
(Of course, if hard to say, also not only use for-in, but for-in to be convenient)
The first piece of the function is actually simple.
1. Do not need to return the value before the function name, the parameter list does not need write type.
2. Variables defined within a function are local variables and cannot be invoked outside.
So the format is basically like this:
Function name (argument list) {
xxxxxxx
[Return xxxx;] Dispensable
}
OK, we know the format, let's try this:

Copy Code code 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>

Output only the number Oh pro, that is, the second line that directly ignore (true tragedy AH), even undefined standards are not reached. Because it is a local variable. is discarded after the function has been executed.

In addition to this normative writing, there is a rebellion, that is, the definition of the function does not write parameters, but the function body can also use parameters, so that when there are parameters, you can have output, at this time because the use of the parameters do not have a name, are all stored in the arguments array. As an example:

Copy Code code 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 properties of the script node, in fact, that is to say can also declare JavaScript. Language must be capitalized

Look at the output:

Note that within the test function, the data stored in the arguments is also possible, such as

Copy Code code as follows:

<script language= "JavaScript" >
<!--
function Add () {
return arguments[0]+arguments[1];
}
document.write (Add (4,44) + "");
document.write (Add (4, "E55"));
-->
</script>

Will output 4e55. Of course, because the function body used two parameters, so if you call only give a parameter, the result is not only output a value, and interested in their own test ~ if you call the 3 parameters, then the third parameter is mercilessly ignored.
In addition to this kind of well-defined definition, there are some other ways to define functions, more non-mainstream, I do not like to use, but still need to write out to understand:
One is: var add=new function ("parameter", "parameter",......, "function Body");
Copy Code code 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, you can see, the last sentence does not need to add a semicolon, the wood has a problem, here the truth is that the function is actually a class, and then add becomes the name of the function.
There is also a way of writing:
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var show=function (name) {
document.write ("<br/>" + "HELLP" +name);
}
Show ("Dumpling");
-->
</script>

The output is understood by everyone. In fact, this writing is to write the letter to the front of the word.
Since it is proved that a function is actually an object, then of course it also has some function functions to call, such as ToString () or valueof () function can be completed, length can return the number of parameters of the function.
Let's try this:
Copy Code code 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 the second article will be here ~ tomorrow continue (/^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.