JavaScript的语法、数据类型、基本算数和逻辑运算操做

str.toString()html

能够把字符串、数值、布尔值、对象转为字符串浏览器

String(str)spa

任何数值强制转换为字符串类型code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        var num=123;
        console.log(typeof num.toString());//string
        console.log(typeof String(num));//string
    </script>
</body>
</html>

其余类型转布尔值htm

一、数值型,除了0,其他都是转为true对象

二、字符串型,除了""空字符串,其他都是转为trueblog

三、null 和 undefined 转为falseip

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        var num;
        console.log(Boolean(num));//false
    </script>
</body>
</html>

在进行算数运算时,浏览器会尝试隐式转换字符串字符串

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        var num1=1;
        var num2="30";
        console.log(num1+num2);//130  此处+为字符串拼接
        console.log(typeof(num1+num2));//string
        console.log(num1*num2);//30
        console.log(typeof(num1*num2));//number
    </script>
</body>
</html>

++num 先递增,再运算string

num++ 先运算,再递增

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        var num1=2;
        var num2=3;
        var num3=++num1+num2++;
        console.log(num1);//3
        console.log(num2);//4
        console.log(num3);//6
    </script>
</body>
</html>

== 和 ===

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        console.log(null==undefined);//true  undefined派生自null
        console.log(null===undefined);//false  是两种不一样的基本类型
    </script>
</body>
</html>

三元运算符能够简化部分if判断的语句

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        var score=85;
        var result=score>60?'及格':'不及格';
        console.log(result);//及格
    </script>
</body>
</html>

&&

若是都是布尔值,则全部布尔值都为true时,返回true;有一个为false则返回false

若是不都是布尔值,则:

若是第一个隐式转换后是true,则返回第二个值;若是第二个也隐式转换为true,则返回第三个;以此类推

若是第一个隐式转换后是false,则返回第一个值

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        
        console.log(15 && 'hello~');//hello~
        console.log(0 && 'hello~');//0
        console.log(25 && 'str' && null);//null
    </script>
</body>
</html>

||

若是都是布尔值,有一个为true,则返回true

所有都是false,则返回false;

若是不都是布尔值,则第一个数值隐式转换为true,就返回第一个数值;不然依次日后

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        
        console.log(15 || 'hello~' || NaN);//15
        console.log(0 || undefined);//undefined
        console.log('' || 'str' || null);//str
    </script>
</body>
</html>

! 取反

!! 取反再取反

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        
        console.log(!null);//true
        console.log(!!null);//false
    </script>
</body>
</html>
相关文章
相关标签/搜索