Javascipt Basic Content--details to be noted

Source: Internet
Author: User
Tags logical operators

javascipt-Basic ---details need to be noted:

1, special values : NaN, Infinity, isNaN (), Isfinite ()

NaN:

Copy Code code as follows:

var a=parseint (' A123 ');
Window.alert (a); Output Nan

Infinity:

Copy Code code as follows:

Window.alert (6/0);//Output Infinity infinity (best not to write)

isNaN (): Judge whether the number, not the number returns TRUE, the number returns false

Copy Code code as follows:

var a= "DD";
Window.alert (isNaN (a)); Returns True

Isfinite (): Used to determine whether infinity. Returns False if number is NaN (not a number), or is a positive or negative infinity.

Copy Code code as follows:

Window.alert (Isfinite (6/1)); Returns True
Window.alert (Isfinite (6/0)); return False

2. Logical operators:

In a logical operation, 0, "", false, null, undefined, and nan all represent false

(or | | ) | | Returns the first value that is not false (the object is also available), or the last value (if all false)

This knowledge point is used a lot in the JavaScript framework.

A

Copy Code code as follows:

var a=true;
var B=false;
var C=b | | A

Window.alert (c); Output true

B

Copy Code code as follows:

var a=2;
var b=0
var c= a | | b

Window.alert (c); Returns the first value, Output 2

C

Copy Code code as follows:

var A=false;
var b= "";
var c = 0;
var d =new Object (); Object

var Aa=a | | B | | C | | D; A,b,c is all false this returns D
Window.alert (AA); Return D (Object)

4. Multi-branch Switch

Copy Code code as follows:

var flag=1;

Switch (flag) {

Default
Window.alert ("nothing");

Case ' a ':
Window.alert ("a");

Case ' B ':
Window.alert ("B"); No break statement, no match succeeded, at which time the result is output

}

Copy Code code as follows:

var flag=1;

Switch (flag) {

Default
Window.alert ("nothing");

Case ' a ':
Window.alert ("a");

Case 1:
Window.alert ("B"); No break statement when the match succeeds then the break statement is not found at this time output b

}

5. Function call

Func.js

Copy Code code as follows:

Function ABC (val) {

Window.alert ("ABC ()" +val);
}

Functions that have return values
function test (num1,num2) {

var res=0;
Res =num1+num2;

return res;
}

Functions that do not return a value
function Noval (num1,num2) {

var res=0;
res=num1+num2;
}

Function call:

Copy Code code as follows:

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >
<script type= "Text/javascript" src= "Func.js" ></script>
<script type= "Text/javascript" >

Function call 1---Normal call (commonly called functions)
ABC (50);

Function Call 2---variable = functions name; Call when called: variable (actual argument)
var test1=abc; At this point the variable is equivalent to a function reference (pointer)
Window.alert (ABC); Output ABC the entire function code, you can understand
Test1 (500);

If the called function has a return value, it can be returned directly in the program without a return value but you received it, which is returned to undefined
Calling functions that have return values
var res=test (20,40);
Window.alert (RES);

Call function with no return value
Window.alert ("Call function with no return value");
var res=noval (1,1);
The output undefined at this time
Window.alert (RES);

</script>
<body></body>

JS supports functions with variable number of parameters

Copy Code code as follows:

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >
<script type= "Text/javascript" src= "Func.js" ></script>
<script type= "Text/javascript" >

Function call-Recursive

/* Function ABC (num) {

if (num>3) {

ABC (--NUM);
}
Document.writeln (num);
}

Call function
ABC (5); Output 3 3 4
*/

JS supports functions with variable number of parameters

Function abc () {
JS provides a arguments that can be accessed so the values passed in
Window.alert (arguments.length); Incoming number of
Iterate through the arguments passed in
for (Var i=0;i<arguments.length;i++) {

Window.alert (Arguments[i]);
}
}

Call
Window.alert ("ABC (12,13,\" hello\ ", 56)");
ABC (12,13, "Hello", 56)

Window.alert ("ABC (5)");
ABC (5);

Window.alert ("ABC ()");
ABC ();
</script>
<body></body>

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.