Home > Web Front-end > JS Tutorial > Basic content of javascipt--details that need attention_Basic knowledge

Basic content of javascipt--details that need attention_Basic knowledge

WBOY
Release: 2016-05-16 17:37:49
Original
1163 people have browsed it

javascipt-Basics---Details that need attention:

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

NaN:

Copy code The code is as follows:

var a=parseInt('a123 ');
window.alert(a); //Output NaN

Infinity:

Copy code The code is as follows:

window.alert(6/0 );//Output Infinity (it’s best not to write it like this)

isNaN(): Determine whether it is a number. If it is not a number, it returns true. If it is a number, it returns false.

var a="dd";window.alert(isNaN(a)); //Return true isFinite(): Used to determine whether it is infinite. Returns false if number is NaN (not a number), or a positive or negative infinity number.


Copy code

The code is as follows:

window.alert(isFinite(6/1)); //Return truewindow.alert(isFinite(6/0)); //Return false 2. Logical operators:

In logical operations, 0, "", false, null, undefined, and NaN all represent false

(or || ) || will return the first value that is not false (objects are also acceptable), or the last value (if all are false)

This knowledge point is used a lot in JavaScript frameworks. a、

Copy code

The code is as follows:

var a=true;var b=false ;var c=b || a; window.alert(c); //Output true

b、


Copy code

The code is as follows:

var a=2;var b=0 var c= a || b; window.alert(c); //Return the first value, output 2

c、


Copy code

The 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,c are all false and this returns d
window.alert(aa); //returns d (object)




4. Multi-branch switch



Copy code

The code is as follows:

var flag=1; switch(flag){default:window.alert("nothing");
case 'a':
window.alert("a ");

case 'b':
window.alert("b"); //There is no break statement and no match is successful. At this time, the results are output

}





Copy code

The code is as follows:

var flag=1; switch(flag){default:window.alert("nothing");
case 'a':
window.alert("a ");

case 1:
window.alert("b"); //There is no break statement. When the match is successful, no break statement will be found. At this time, b

}


5. Function call



func.js

Copy code

The code is as follows:

function abc(val){ window.alert("abc()" val);} //Function with return value
function test(num1,num2){

var res=0;
res =num1 num2;

return res;}


//Function without return value

function noVal(num1,num2){


var res=0;

res=num1 num2;

}


Function call:




Copy code

The code is as follows:










js supports functions with variable number of parameters

Copy code The code is as follows:








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