Home > Web Front-end > JS Tutorial > JavaScript Basics 2 Data Types, Statements, Functions_Basic Knowledge

JavaScript Basics 2 Data Types, Statements, Functions_Basic Knowledge

WBOY
Release: 2016-05-16 17:55:29
Original
1095 people have browsed it

A word of nonsense before writing the content: Because some older browsers do not support javascript scripts, when encountering a script node, it is output as ordinary content. Therefore, sometimes in order to make the version compatible, comment symbols will be written on the content in the script node, like this In the old version, although the program will fail, our code will not appear, similar to this (please ignore the brackets and the things in the brackets):

Copy code The code is as follows:



Note: After testing, it was found that js statements cannot be written with comment symbols. On the same line, otherwise it will not work in new browsers.

Javascript data types:

Numeric types: including all numbers.
String type string is represented by "" or ''.
Boolean type boolean =true or false.

Variable: A container used to store data. The value stored in the program can be changed.

Declaration of variables: var variable name [= value];

If the variable is declared inside the function, then it is local. If it is outside the function, it is global. You know the meaning? of.

In other words, no matter what type of variable is declared, var is used. The specific data type is determined after initialization. If it is not initialized, it is a variable of undefined type.

Let’s use the small project mentioned in the previous js article to play with its numerical type.

Write in main.html:
Copy the code The code is as follows:



O.O



This is my JSP page.




Then we look at the output:

As you can see, after assignment, var has its own type, but undefined 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 these three, it can also return the type of object.

But if it is a special type, instanceof is needed to return the type name.

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

data Type conversion:

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

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

var iNum=30; 30.toString(2);// Add a 2 in the following brackets, which means converting to a binary string.

//You can also have octal and hexadecimal. If there are no numbers, it will be converted directly.

The next step is to do a little experiment. Change the content under the script node in the file just now:
Copy the code The code is as follows:



Then, run it~

It seems that converting types is actually very simple~
Operations:
Operations are actually the same as those in C Java, they are all addition, subtraction, multipliers, and, or, There is actually no difference.
Let me reiterate the difference between i and i here.
i executes i 1 once, but returns i. For example, if I write a sentence
Copy the code The code is as follows:

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

Then the runtime sequence is actually: assignment : i=50, judgment: if(i<=50), execute i=i 1, execute the statement after judgment: xxxx. So the final output is 51, so I won’t take a screenshot.
If i is used there, the value returned is the value after adding 1, which means there will be no output.

-------------------------------------------------- ------------------------------------
Statement part
Conditional statements: if and switch
In fact, it is the same as c. This part does not need to be explained in detail. Even in the conditional statement, you should also pay attention to some small details:
In the if statement of js, false is returned in the following situations: null undefined empty character String "" 0 false
Also note about the empty string: var s="" and var s=new String("") are different. The latter opens up memory space, so it returns true.
Let’s verify it~, under the javascript node:
Copy the code The code is as follows:



Then save and refresh the main page:

Oh ( ⊙ o ⊙ ), as expected, only new string appeared~.

Swith statement also try:
Copy code The code is as follows:



View output:

OK, next is the loop statement: while do-while for-in for
The only difference between the first two is that the order of looping and judgment is different, do-while loops one more time than while , I won’t give an example.
I believe everyone is familiar with the for loop. Let’s look at the sentence for-in.
This is actually for arrays. The initialization of arrays in js is also quite strange. For example, we write it in the script node: (Also note the initialization of the array, using square brackets)
Copy code The code is as follows:



Let’s view the output:

From this result, we can explore a few points:
Among them, test is actually an int number to represent the number of the array.
for-in can only go through one number each time it loops, and is generally used for exhaustive enumeration.
In some cases, you can only use for-in for exhaustive enumeration. For example, the contents stored in the array include strings and numbers.
(Of course, if you insist on it, you don’t have to use for-in, but for-in is much more convenient)
Function is actually briefly mentioned in the first article.
1. There is no need to return a value before the function name, and there is no need to write a type in the parameter list.
2. Variables defined inside the function are local variables and cannot be called outside.
So the format is basically like this:
function function name (parameter list){
xxxxxxx;
[return xxxx;] //optional
}
OK, got it Now that we have the format, let’s try it:

Copy code The code is as follows:



The output is only numbers, dear, that is to say, the second line is ignored directly (what a tragedy), and it does not even meet the undefined standard. Because it is a local variable. It is discarded after the function is executed.

In addition to this standard writing method, there is also a rebellious way, that is, there are no parameters when defining a function, but parameters can also be used in the function body. In this way, when there are parameters, there can be output. This Because the parameters used have no names, they are all stored in the arguments array. For example:

Copy code The code is as follows:



PS: I changed the attributes in the script node , in fact, it means that javascript can also be declared like this. LANGUAGE must be capitalized

Look at the output:

Note that here, within the tested function, it is also possible to use the data stored in arguments to perform operations, such as

Copy code The code is as follows:



will output 48 4e55. Of course, because two parameters are used in the function body, if you only give one parameter when calling, the result will not output only one value. If you are interested in testing it yourself~ If you give 3 parameters when calling, Then the third parameter is ruthlessly ignored.
In addition to the above fairly regular definition, there are some other ways to define functions, which are relatively non-mainstream and I don’t like to use them, but I still have to write them down to understand:
One is: var add =new Function("parameter","parameter",...,"function body");
Copy code The code is as follows:



The output is correct. You can see that there is no need to add a semicolon in the last sentence. There is no problem. The reason here is that Function is actually a class, and then add becomes the name of the function.
There is another way to write:
Copy the code The code is as follows:



Output everyone understands. . This way of writing is actually to write the function name first.
Now that it has been proven that the function is actually an object, of course it also has some functional functions that can be called, such as the toString() or valueOf() function that can be typed completely, and length can return the number of parameters of the function.
Let’s try it:
Copy code The code is as follows:



Output:
Hello Dumpling
function (name){ document.write("Hello " name); }
number of arguments: 1
OK That’s it for the second article ~ continue tomorrow (/^o^)/
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template