当前位置:网站首页>探索一下自增自减运算符的使用方式
探索一下自增自减运算符的使用方式
2022-07-19 05:22:00 【终极小腾】
1.引言
递增运算符( ++ )和递减运算符( – )
为了区别x++和++x的区别故写此篇。
2.用JavaScript代码实现
自增运算
//自增,自增的意思就是本身加上1所得的数字
var x = 5;
x++;//x++等同于x = x + 1;
console.log(x);//结果为6
// X++ 与 ++x是两种用法
var a1 =5 , a2 = 5 ;
var b;
var c;
b=a1++;
c=++a2;
console.log(b);//输出5
console.log(c);//输出6
console.log(a1);//输出6
console.log(a2);//输出6
通过上述代码我们可以发现,变量值为5,无论通过变量++
还是++变量
,变量的值都自加1,但是自加过程中赋值给其他变量时却出现了差异。
其实代码的底层原理我们不需要知道,有一个规律可以记一下,等号右边靠近谁,就用谁。
例如,s = x++
这个等号右边是变量,那么就先把变量赋值过去;(先使用后自加)s = ++x
这个等号右边是加号,那么就先运算自加,得出结果再赋值;(先自加后使用)
同理自减也是这样
var x1 =9 , x2 = 9 ;
var y;
var z;
y=x1--;
z=--x2;
console.log(y);//输出9
console.log(z);//输出8
console.log(x1);//输出8
console.log(x2);//输出8
等号右边靠近谁,就用谁。s = x--
这个等号右边是变量,那么就先把变量赋值过去。(先使用后自减)s = --x
这个等号右边是减号,那么就先运算自减,得出结果再赋值。(先自减后使用)
3.小结
总体的区别就是- - -(先运算再使用)与- - -(先使用再运算)的区别。
加以练习就能得到深刻印象。
边栏推荐
猜你喜欢
Win10 + CUDA11.7+pytorch手动安装-2.0版本
Kubernetes终端管理工具 — KubeBox
斐讯k1刷入Breed以及openwrt的教程
Insert cross column pictures under the title of the home page of latex IEEE paper, and solve the footnote problem
Unable to get browser (Selenium::WebDriver::Error::NoSuchWindowError)
Day006选择结构(if语句练习)
Gocore-v2 framework API interface development concept
关于正向代理和反向代理的理解
论文笔记:Self-critical Sequence Training for Image Captioning
DOM事件绑定
随机推荐
Solr search engine - Chinese full spelling and simplified word segmentation
狂神。MySql学习。
[resource record] what is reversible neural networks and its relationship with VAE and Gan; What are bits per pixel, bits per dim
DevOps:从「蒸汽时代」到「高铁时代」,SUNMI DevOps转型之路
[resource record] Introduction to Bayesian neural network (BNN), common packages and differences
DOM与事件
解决几个常见问题
平台化如何助力研发效率提升?
Solr搜索引擎 — SolrCloud安装和集群配置
System safety and Application
猫和狗的分类例子-Kaggle
How to do unit testing well? Golang mock "three swordsmen" gomock, monkey, sqlmock
Redash - a powerful open source data visualization platform
今天整理了一些关于高度塌陷的问题
Summary 2 - deep learning network building learning
Nat overview
ORM常见需求
下拉框三角的简易设置
Scala案例(伴生对象)
Pytoch: dataset summary