08

    技术2022-07-10  116

    算数运算符: + - * / %

    1.加法运算的注意点 (1). 任何非数值类型的数据在参与加法运算之前,都会被自动转换为数值类型后再参与运算

    <script> let flag = true; let num = 100; let sum = flag + num; console.log(sum); </script>

    控制台输出: (2)任何数据和NaN进行运算,结果都是NaN

    <script> let flag; let num = 100; let sum = flag + num; console.log(sum); </script>

    控制台输出: (3)任何数据和字符串相加,都会被先转换成字符串之后再运算。(类似字符串拼接)

    <script> let str = "123"; let num = 100; let sum = str + num; console.log(sum); </script>

    控制台输出:

    2.减法 / 乘法 / 除法 运算的注意点 (1). 任何非数值类型的数据在参与加法运算之前,都会被自动转换为数值类型后再参与运算

    <script> let flag = true; let num = 100; let sum = flag - num; console.log(sum); </script>

    控制台输出: (2)任何数据和NaN进行运算,结果都是NaN

    <script> let flag; let num = 100; let sum = flag - num; console.log(sum); </script>

    (3)任何数据和字符串相减,都会被先把字符串转换成数值类型之后再运算。

    <script> let str = "123"; let num = 100; let sum = str - num; console.log(sum); </script>

    3.取模运算的注意点 格式: m % n = 余数

    (1) 如果m > n,就正常取余数

    <script> let m = 10; let n = 3; let result = m % n; console.log(result); </script>

    控制台输出: (2) 如果m < n, 那么结果就是m

    <script> let m = 10; let n = 3; let result = n % m; console.log(result); </script>

    控制台输出: (3) 如果n = 0 ,那么结果就是NaN

    <script> let m = 10; let n = 0; let result = m % n; console.log(result); </script>

    控制台输出: (4) 取余运算结果的正负性,取决于m而不是n

    <script> let m = -10; let n = 3; let result = m % n; console.log(result); </script>

    控制台输出:

    <script> let m = 10; let n = -3; let result = m % n; console.log(result); </script>

    控制台输出:

    Processed: 0.025, SQL: 9