zoukankan      html  css  js  c++  java
  • JavaScript的语法、数据类型、基本算数和逻辑运算操作

    str.toString()

    可以把字符串、数值、布尔值、对象转为字符串

    String(str)

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

    <!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>

    其他类型转布尔值

    1、数值型,除了0,其余都是转为true

    2、字符串型,除了""空字符串,其余都是转为true

    3、null 和 undefined 转为false

    <!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 先递增,再运算

    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>
  • 相关阅读:
    一起谈.NET技术,JAVA与.NET的相互调用——TCP/IP相互调用基本架构 狼人:
    一起谈.NET技术,.NET异步编程:IO完成端口与BeginRead 狼人:
    一起谈.NET技术,VS2010自定义新建文件模版 狼人:
    一起谈.NET技术,理解.NET程序集的执行过程 狼人:
    一起谈.NET技术,JAVA与.NET的相互调用——利用JNBridge桥接模式实现远程通讯 狼人:
    一起谈.NET技术,用NuGet掌管你的Visual Studio扩展 狼人:
    一起谈.NET技术,Visual Studio 2010 中的代码约定设置 狼人:
    一起谈.NET技术,C#中的语言特性都是从何而来? 狼人:
    一起谈.NET技术,关于Winform下,获取Treeview中CheckBox选中项的技巧 狼人:
    一起谈.NET技术,ASP.NET MVC 3和Razor中的@helper 语法 狼人:
  • 原文地址:https://www.cnblogs.com/chenyingying0/p/12259197.html
Copyright © 2011-2022 走看看