JavaScript一元运算符

Js入门与实战 admin 494浏览

一元运算符,++或- -对自身操作加1或减1;

<script>
var a = 5;
// a++;
// a--;
// ++a;
--a;
console.log(a);
</script>

操作符在变量前面,先进行自身运算再进行其他运算;操作符在变量后面,先进行其他运算再进行自身运算;

<script>
var b = 5;
++b;
var c = 6;
console.log(b+c++);
console.log(b);
console.log(c);
</script>
<script>
var d = 1;
var e = ++d+d++;
console.log(d);
console.log(e);

var f = 1;
var g = f++ + ++f;
console.log(g);
console.log(f);

var h = 1;
var i = ++h + ++h;
console.log(i);
console.log(h);
</script>

转载请注明:大灰牛博客 » JavaScript一元运算符