JS基础_算数运算符
<!DOCTYPE html>
<html>
<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;
console.log(result);//number
console.log(typeof result); //string result = a + 1;
console.log(result);//124 result = 456 + 789;
console.log(result);//1245 result = true + 1;
console.log(result);//2 //任何的值和字符串做加法运算,都会先转换为字符串,然后再和字符串做拼串的操作
result = true + false;
console.log(result);//1 //任何的值和字符串做加法运算,都会先转换为字符串,然后再和字符串做拼串的操作
result = 2 + null;
console.log(result);//2 //任何值和NaN做运算都得NaN
result = 2 + NaN;
console.log(result);//NaN //如果对两个字符串进行加法运算,则会做拼串,会将两个字符串拼接为一个字符串,并返回
result = "123" + "456";
console.log(result);//123456 result = "你好" + "大帅哥";
console.log(result);//你好大帅哥 var str = "锄禾日当午," +
"汗滴禾下土," +
"谁知盘中餐," +
"粒粒皆辛苦";
console.log(str);//锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦 //----------------------------------------------------------------------------------- //任何的值和字符串做加法运算,都会先转换为字符串,然后再和字符串做拼串的操作
result = 123 + "1";
console.log(result);//1231 //任何的值和字符串做加法运算,都会先转换为字符串,然后再和字符串做拼串的操作
result = true + "hello";
console.log(result);//truehello /*
* 我们可以利用这一特点,来将一个任意的数据类型转换为String
* 我们只需要为任意的数据类型 + 一个 "" 即可将其转换为String
* 这是一种隐式的类型转换,由浏览器自动完成,实际上它也是调用String()函数
*/
var c = 123;
c = c + ""; //和c = String(c)本质上是一样的
console.log(typeof c);//string result = 1 + 2 + "3";
console.log(result); //33 result = "1" + 2 + 3;
console.log(result); //123 //----------------------------------------------------------------------------------- result = 100 - 5;
console.log(result); //95 result = 100 - true;
console.log(result); //99 result = 100 - "1";
console.log(result); //99 result = 2 * 2;
console.log(result); //4 result = 2 * "8";
console.log(result); //16 result = 2 * undefined;
console.log(result); //NaN result = 2 * null;
console.log(result); //0 result = 4 / 2;
console.log(result); //2 result = 3 / 2;
console.log(result); //1.5 //----------------------------------------------------------------------------------- /*
* 任何值做 - * / 运算时都会自动转换为Number
* 我们可以利用这一特点做隐式的类型转换
* 可以通过为一个值 -0 、*1 、 /1来将其转换为Number,原理和Number()函数一样,使用起来更加简单
*/ var d = "123";
d = d - 0;
console.log(d); //123
console.log(typeof d);//number result = 9 % 3;
console.log(result); //0 result = 9 % 4;
console.log(result); //1 result = 9 % 5;
console.log(result); //4 </script>
</head>
<body>
</body>
</html>
JS基础_算数运算符的更多相关文章
- JS基础_相等运算符
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JS基础_关系运算符
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JS基础6--逻辑运算符
&&与 ||或 !非 如果对一个值进行两次取反,它不会变化 如果对一个非布尔值进行取反,则会将其转换为布尔值,再取反 所以我们可以利用该特点.来将 ...
- JS基础_运算符的优先级
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JavaScript基础一(js基础函数与运算符)
[使用js的三种方式] 1.在HTML标签中,直接内嵌js(并不提倡使用) <button onclick=" alert('点就点')"> 点我啊</butto ...
- JS基础_属性名和属性值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JS基础_数据类型-Number类型
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JS基础_标识符
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JS基础_全局作用域
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
随机推荐
- Java获取当前时间及String、datetime、date相互转化
一.获取当前系统时间和日期并格式化输出: import java.util.Date; import java.text.SimpleDateFormat; public class NowStrin ...
- github pr
github----向开源框架提交pr的过程 https://blog.csdn.net/vim_wj/article/details/78300239github 的 pr 使用方法 https:/ ...
- 人才-T型人才:百科
ylbtech-人才-T型人才:百科 T型人才是指按知识结构区分出来的一种新型人才类型.用字母“T”来表示他们的知识结构特点.“—”表示有广博的知识面,“|”表示知识的深度.两者的结合,既有较深的专业 ...
- HttpClient提交数据
用代码模拟浏览器的行为 * 轻量级的开源的框架 * Android在6.0 23 以后移除了httpclient ,所以开发中用的少了 * 编写步骤: 1. 打开浏览器 2. 输入网址 3. 敲回车 ...
- Docker容器时间与主机时间相差8小时
查看主机时间 [root@localhost ~]# date 2016年 07月 27日 星期三 22:42:44 CST 查看容器时间 root@b43340ecf5ef:/# date Wed ...
- Python2.7 threading模块学习
主要学习一下python的多线程编程,使用threading模块,threading 包括:Thread.conditions.event.rlock.semaphore等类. Thread对象可以实 ...
- Delphi动态事件深入分析
[delphi] view plain copy print? 首先做一个窗体如下 然后单元中如下代码: 在implementation下面声明两个方法如下: //外部方法,只声明一个参数,此时按 ...
- htop/dstat/top/ps命令的使用
top命令 显示系统中进程信息 [root@node0 ~]# top top - 09:36:45 up 13:39, 3 users, load average: 0.02, 0.03, 0. ...
- 【jQuery】attr()、prop()、css() 的区别(转载)
.attr( ) 可以设置元素的属性(也就是给元素新增加一个原来并不存在的属性)也可以获取元素的本来就有的属性以及额外设置的属性.如果要获取的属性没有设置,那么获取到的结果是 undefined; . ...
- 模型蒸馏(Distil)及mnist实践
结论:蒸馏是个好方法. 模型压缩/蒸馏在论文<Model Compression>及<Distilling the Knowledge in a Neural Network> ...