<!--Mathod 1--> <head> <script src="js_path"></script> </head> <!--Mathod 2--> <body> <script> statement </script> </body>
如果不用var声明,则该变量默认为全局变量
var a=1; var a=1, b=2; var s="hello",f=3.2;
自定义标识符: sayHello
字符串常量: "nihao"
保留字: function; var
<script> function sayHello(){ var sHelloFirstArg="nihao" document.write(sHelloFirstArg) } sayHello(); </script>
字符串拼接引起的自动转换
转换函数
NaN: 当字符串转换数字失效时,会得到NaN这个值,归属于Number类型。但NaN是一个空值,不会和任何数据包括它自己相等
parseInt(); //将数据转换成整型 typeof(); // 返回数据类型
// 基本运算符(+ - * / ++ --) var a = 1; var b = a++ // b=1 var b = ++a // b=2
If "b" is the string of digital type, it will be automatically turned back the "b" of digital type but will get a NAN value.
var a = +b var a = -b
"&&" and "!!" can get a value not belonging the boolen type, if the result on one side is not the type of boolen.
// && !! ! 2>1 && 3>5 // false(Equate "and" in python) 2>1 !! 3>5 // true(Equate "or" in python) 2 ! [3,5] // true(Equate "not" in python)
&: only when the two number are 1, the counting result equate 1,but 0 in binary system's counting
|: the result of same number is itself. but 1 in binary system's counting
^: the result of same number is 0. but 1 in binary system's counting
// all numbers shift two digits westwards, and the empty position will be filled by zero in the binary system << // all numbers shift two digits eastwards, and the empty position will be filled by zero in the binary system >> // all zero and one exchange position for all numbers ~ // 位与(&) 1 & 2 = 0 /* 0 1 1 0 */ // 位或(|) 1 | 2 = 3 /* 0 1 1 0 */ // 异或(^) 1 ^ 2 = 3 /* 0 1 1 0 */
notice
null == undefined return ture
before checked if two characters are equation, all counting numbers tend to become the type of Number,having a priority of switch.
// one of the two characters Will change type if the two characters are the type that can be switched by each other,then return true or false == != // one of the two characters Will not change type, then return true or false === !==
// > < >= <= alert(25<3) // false alert("25"<"3") // true (Each characters base on the position on the ASCI to judge, if the comparation have not the result) alert("25"<3) // false
flase; []; 0; null; undefined;
if (condition){ statement; }else if(condition){ statement; }else{ statement; }
var x = 3; switch(x){ case 1:statement; break; case 2:statement; break; case 3:statement; break; default:statement; }
// Method 1 var a=[1,"hello",true]; for (var i in a){ console.log(i); console.log(a[i]); } // Method 2 for (var i=1; i<10; i=i+1){ console.log(i) }
var sum = 0; var flag = 1; while(flag<101){ sum += flag; ++flag; }
throw Error("xxx") (Artificially thorw the information of error)
try{ statement; } catch(e){ statement; } finally{ statement; }