前端-JavaScript1-7——JavaScript之数学运算符

---恢复内容开始---

运算符叫做operator,也可以叫做操作符。运算符有很多种,一元运算符、二元运算符;数学运算符、逻辑运算符……我们今天先学习数学运算符,比较简单

+     加法

-      减法

*     乘法

/      除法

%    取余数

( )    括号

下面的结果都是3:

1           console.log(1 + 2);

2           console.log(8 - 5);

3           console.log(1.5 * 2);

4           console.log(12 / 4);

5           console.log(13 % 5);  //得几不重要,要的是余数

取余数这个运算,实际上也是除,要的是余数:

1    //取余数

2    console.log(12 % 3);  //0

3    console.log(121 % 11);   //0

4    console.log(5 % 8);      //5

5    console.log(8 % 5);      //3

6    console.log(5 % 5);      //0

默认的计算顺序,先乘除,后加减。乘除取余是平级,先遇见谁,就算谁

1    console.log(1 + 2 * 3);      //7

2    console.log(1 + 2 * 3 % 3);  //1

3    console.log(1 + 2 % 3 * 3);  //7

我们可以用小括号来改变计算先后顺序,注意没有中括号和大括号,一律用小括号。

1    var a = 4 * (3 + (1 + 2) * 3);

2    alert(a);

特殊的数字运算,很多公司在考,考你对面试的重视程度,因为这个知识实战中一辈子用不到。

JS中的数学运算符,就这么几个,如果你学过C,那么我提醒你,没有乘方^。如果你学过java那么我提醒你,没有取整除法\。

乘法怎么算?

1    Math.pow(3,4);

这是一个新的API,记住就行了,后面的课程将会告诉你,Math是一个内置对象,pow是它的一个方法。

Math就是数学,pow就是power乘方。

1    var a = Math.pow(3,2 + Math.pow(3,6));

根号就是

1    Math.sqrt(81);

今天遇见的所有API:

alert("哈哈");

prompt("请输入数字","默认值");

console.log("哈哈");

parseInt("123",8);

parseFloat("123");

Math.pow(3,4);

Math.sqrt(81);

最后附上代码

运算符的模样

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <script type="text/javascript">
        // console.log(1 + 2);
        // console.log(8 - 5);
        // console.log(1.5 * 2);
        // console.log(12 / 4);
        // console.log(13 % 5);    //13÷5=2……3 得几不重要,要的是余数

        // //取余数
        // console.log(12 % 3);    //0
        // console.log(121 % 11);    //0
        // console.log(5 % 8);        //5
        // console.log(8 % 5);        //3
        // console.log(5 % 5);        //0

        // console.log(1 + 2 * 3);        //7
        // console.log(1 + 2 * 3 % 3);    //1
        // console.log(1 + 2 % 3 * 3);    //7

        // var a = 4 * (3 + (1 + 2) * 3);
        // alert(a);

        // 隐式的类型转换
                var a = "3" * 8;
                console.log(a);
    </script>
</head>
<body>
    
</body>
</html>

乘法运算符的样子

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <script type="text/javascript">
        var a = Math.pow(3,2 + Math.pow(3,4));
        console.log(a);
    </script>
</head>
<body>
    
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/jingyao/p/10180908.html
今日推荐