Javascipt basic content-details to be aware

Source: Internet
Author: User

Javascip-basic--- Notes for details:

1. Special values: NaN, Infinity, isNaN (), isFinite ()

NaN:

Copy codeThe Code is as follows: var a = parseInt ('a123 ');
Window. alert (a); // output NaN

Infinity:

Copy codeThe Code is as follows: window. alert (6/0); // output Infinity (it is best not to write it like this)

IsNaN (): determines whether it is a number. If it is not a number, true is returned. If it is a number, false is returned.

Copy codeThe Code is as follows: var a = "dd ";
Window. alert (isNaN (a); // return true

IsFinite (): used to determine whether it is infinite. If number is NaN (not a number) or positive or negative infinity, false is returned.

Copy codeThe Code is as follows: window. alert (isFinite (6/1); // return true
Window. alert (isFinite (6/0); // return false

2. logical operators:

In logical operations, 0, "", false, null, undefined, and NaN indicate false.

(Or |) | returns the first value (the object can also be) other than false, or the last value (if all values are false)

This knowledge point is widely used in javascript frameworks.

A,

Copy codeThe Code is as follows: var a = true;
Var B = false;
Var c = B |;

Window. alert (c); // Output true

B,

Copy codeCode: var a = 2;
Var B = 0
Var c = a | B;

Window. alert (c); // returns the first value and outputs 2

C,

Copy codeThe Code is as follows: var a = false;
Var B = "";
Var c = 0;
Var d = new Object (); // Object

Var aa = a | B | c | d; // a, B, and c are all false.
Window. alert (aa); // return d (object)

4. multi-branch switch

Copy codeThe Code is as follows: var flag = 1;

Switch (flag ){

Default:
Window. alert ("nothing ");

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

Case 'B ':
Window. alert ("B"); // if no break statement exists and no matching is successful, all results are output.

}

Copy codeThe Code is as follows: var flag = 1;

Switch (flag ){

Default:
Window. alert ("nothing ");

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

Case 1:
Window. alert ("B"); // No break statement. If the match is successful, the break statement is no longer found. At this time, B is output.

}

5. function call

Func. js

Copy codeThe Code is as follows: function abc (val ){

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

// Functions with returned values
Function test (num1, num2 ){

Var res = 0;
Res = num1 + num2;

Return res;
}

// Functions without return values
Function noVal (num1, num2 ){

Var res = 0;
Res = num1 + num2;
}

Function call:

Copy codeThe Code is as follows: <Head>
<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 (common call function)
Abc (50 );

// Function call 2 --- variable = function name; call this way: variable (actual parameter)
Var test1 = abc; // at this time, the variable is equivalent to the reference (pointer) of the function)
Window. alert (abc); // output the entire abc function code.
Test1 (500 );

// If the called function has a return value, it can be directly returned in the program. If there is no return value, but you have received it, this is the returned undefined
// Call a function with a returned value
Var res = test (20, 40 );
Window. alert (res );

// Call a function without return values
Window. alert ("calling a function without return values ");
Var res = noVal (1, 1 );
// Output undefined at this time
Window. alert (res );

</Script>
</Head>
<Body> </body>
</Html>

Js supports functions with Variable Parameter count

Copy codeThe Code is as follows: <Head>
<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 -- Recursion

/* Function abc (num ){

If (num> 3 ){

Abc (-- num );
}
Document. writeln (num );
}

// Call a function
Abc (5); // outputs 3 3 4
*/

// Js supports functions with Variable Parameter count

Function abc (){
// An arguments is provided in js to access the input value.
Window. alert (arguments. length); // how many parameters are passed in
// Traverse input parameters
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>
</Head>
<Body> </body>
</Html>

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.