<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>转换为Boolean</title>
<script type="text/javascript">
/*
将其他的数据类型转换为Boolean
使用Boolean()函数
- 数字 ---> 布尔
- 除了0和NaN,其余的都是true
- 字符串 ---> 布尔
- 除了空串,其余的都是true
- null和undefined都会转换为false
- 对象也会转换为true
*/
var a = 123;//true
a = -123;//true
a= 0;//false
a= infinity;//true
a = NaN;//false
a = Boolean(a);
a= "hello";//true
a = "true";//true
a = "false";//true
a= "错误";//true
a = " ";//true
a = "";//false
a = null;//false
a = undefined;//false
a= window;//false
a= undefined;//false
console.log(typeof a);
console.log(a);
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>其他进制的数字</title>
<script type="text/javascript">
var a = 123;
/*
在js中,如果需要表示16进制的数字,则需要以0x开头
如果需要表示8进制的数字,则需要以0(0O)开头
如果要要表示2进制的数字,则需要以0b开头,但是不是所有的浏览器都支持
*/
a = 0x10;//16
a = 0xff;//255
a= 0xcafe;//51966
a= 070;//56
a= 0b10;//2
a= "070";
a = parseint(a,8);
console.log(typeof a);
console.log(a);
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>转换为Number</title>
<script type="text/javascript">
/*
将其他的数据类型转换为Number
转换方式一:
使用Number()函数
转换方式二:
- 这种方式专门用来对付字符串
- parseInt() 把一个字符串转换为一个整数
- parseFloat() 把一个字符串转换为一个浮点数
*/
var a = "123";//123
a = 'abc';//NaN
a = '789abc';//NaN
a = "";//0
a= " ";//0
a = true;//0
a = false;//0
a = undefined;//NaN
a = "123px"; //NaN
a= Number(a);
a=parseInt(a);
a="123px";//123
a = parseInt(a);
a = "123.456px";//123456
a = parseFloat(a);
a= true;//"true"NaN
a = parseInt(a);
console.log(typeof a);
console.log(a);
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>强制类型转换</title>
<script type="text/javascript">
/*
强制类型转换
- 指将一个数据类型强制转换为其他的数据类型
- 类型转换主要指,将其他的数据类型,转换为
String Number Boolean
*/
/*
将其他的数据类型转换为String
*/
var a= 123;//number
var b = a.tostring();
a= true;//Boolean
a= a.tostring();//string
a = null;//object
//a =a.tostring();
a= 123;
a = string(a);
a = null;
a = string(a);//string
console.log(typeof a);
console.log(a);
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Null和Undefined</title>
<script type="text/javascript">
/*
Null(空值)类型的值只有一个,就是null
null这个值专门用来表示一个为空的对象
使用typeof检查一个null值时,会返回object
Undefined(未定义)类型的值只有一个,就undefind
当声明一个变量,但是并不给变量赋值时,它的值就是undefined
使用typeof检查一个undefined时也会返回undefined
*/
var a = null;
console.log(a);
console.log(typeof a);//object
var b;//undefinrd
//b = undefined
console.log(b);
console.log(typeof b);//undefined
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>运算符</title>
<script type="text/javascript">
/*
运算符也叫操作符
通过运算符可以对一个或多个值进行运算,并获取运算结果
比如:typeof就是运算符,可以来获得一个值的类型,它会将该值的类型以字符串的形式返回
"number" "string" "boolean" "undefined" "object"
算数运算符
当对非Number类型的值进行运算时,会将这些值转换为Number然后在运算
任何值和NaN做运算都得NaN
+
+可以对两个值进行加法运算,并将结果返回
如果对两个字符串进行加法运算,则会做拼串,会将两个字符串拼接为一个字符串,并返回
任何的值和字符串做加法运算,都会先转换为字符串,然后再和字符串做拼串的操作
-
- 可以对两个值进行减法运算,并将结果返回
*
* 可以对两个值进行乘法运算
/
/ 可以对两个值进行除法运算
%
% 取模运算(取余数)
*/
var a = 123;
var result = typeof a;//number
console.log(result);
console.log(typeof a);
resule = a+1;//124
result = 456+789;//1245
result = true +1 ;//1+1=2,true
result = true +false;//false 0
result = 2 + null;//2,null 0
result = 2+ NaN;//nan
//任何值和nan做运suan都得nan
result = "123"+"456";//123456
result = "您好"+"帅哥";//"您好帅哥"
//console.log(result);
console.log(result);
var str = "挣钱就像便秘"+"老难了"+"花钱就像拉稀"+"憋不住.";
console.log(str);
result= 123+"1";//1231
result = true + "hello";//truehello
//console.log(result)
var c = 123;
c = string(c);
c= c+"";
//console.log(typeof c);
//console.log(c);
c= 123;
console.log("c = " +c);//c=123
result = 1+2+"3";//33
result = "1"+2+3;//123
result = 100-5;//95
result = 100-true;//99
result = 100 -"1";//99
result = 2*2;//4
result = 2*8;//16
result = 2* undefined;//nan
result = 2* null;//0
result = 4 / 2;//2
result = 3/2;//1.5
console.log("result=" + result);
var d = "132";
//d = number(d);
d = d- 0;//-0 *1 /1
console.log(typeof d);
console.log(d);
result = 9% 3;//0
result = 9% 4;//1
result = 9% 5;//4
console.log("result = " + result);
</script>
</head>
<body>
</body>
</html>