一.变量定义
/* 变量类型 */
var x = 4;
x = "Now it`s a string.";
/* 声明变量 */
var x;
var myVar;
var counter1;
var x, y, zeta;
var x = 1, y = "hello", zeta = 14;
二.变量作用域
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Scoping Example</title>
<script type="text/javascript">
//全局变量
var aNewVariable = "is global.";
function doSometing(incomingBits) {
alert("Global variable within the function: " + aNewVariable);
alert("Local variable within the function: " + incomingBits);
}
</script>
</head>
<body>
<script type="text/javascript">
doSometing("is a local variable");
alert("Global var outside the function: " + aNewVariable);
alert("Local var outside the function: " + incomingBits);
</script>
</body>
</html>