JS基础一
变量类型:typeof()运算符
<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8'>
<title>变量</title>
<script>
var a;
//a=12;
//alert(typeof a); //number
//a='adsd';
//alert(typeof a); //string
//a=3.3;
//alert(typeof a); //number
// a=true;
// alert(typeof a); //boolean
//alert(typeof b); //undefined
// a=document;
// alert(typeof a); //object
//alert(typeof a); //undefined
a=function aaa(){
alert(1);
}
alert(typeof a); //function
</script>
</head>
</html>
强制类型转换parserInt(),parsetFloat()
<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8'>
<title>变量</title>
<script>
window.οnlοad=function(){
var oTxt1=document.getElementById('txt1');
var oTxt2=document.getElementById('txt2');
var oBtn=document.getElementById('btn');
oBtn.οnclick=function()
{
alert(parseInt(oTxt1.value)+parseInt(oTxt2.value));
};
};
</script>
</head>
<body>
<input id='txt1' type="text">
<input id='txt2' type="text">
<input id='btn' type="button" value="求和">
</body>
</html>
隐示类型转换:===不转换直接比较,==先转换再比较
<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8'>
<title>NaN</title>
<script>
var a=1;
var b='1';
function aaa(){
alert(a===b);
}
function bbb(){
alert(a==b);
}
aaa();
bbb();
</script>
</head>
</html>
NaN!=NaN
12+NaN=NaN
isNan()判断是否为NaN
<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8'>
<title>变量</title>
<script>
window.οnlοad=function(){
var oTxt1=document.getElementById('txt1');
var oTxt2=document.getElementById('txt2');
var oBtn=document.getElementById('btn');
oBtn.οnclick=function()
{
var n1=parseInt(oTxt1.value);
var n2=parseInt(oTxt2.value);
if(isNaN(n1)&&!isNaN(n2))
{
alert("第一个数有误!");
}
else if(isNaN(n2)&&!isNaN(n1))
{
alert("第二个数有误!");
}
else if(isNaN(n1)&&isNaN(n2))
{
alert('两个数字都有误!');
}
else{
alert(parseInt(oTxt1.value)+parseInt(oTxt2.value));
}
};
};
</script>
</head>
<body>
<input id='txt1' type="text">
<input id='txt2' type="text">
<input id='btn' type="button" value="求和">
</body>
</html>
变量命名规则
类前缀+首字母大写的名字
比如:类前缀 a 数组,o 对象。